# AnsiStirng 类型数据相减,该如何解决

www.MyException.Cn  网友分享于：2013-04-23  浏览：21次
AnsiStirng 类型数据相减
C/C++ code
```
AnsiString we = "123456.1234";
AnsiString wr = "1.123";
we = we-wr;
ShowMessage(we);  //123455.0004

```

C/C++ code
```
AnsiString we = "123456789123456.1234";
AnsiString wr = "1.123";
we = we-wr;
ShowMessage(we);   //  123456789123455

```

------解决方案--------------------

------解决方案--------------------

------解决方案--------------------

------解决方案--------------------

------解决方案--------------------

------解决方案--------------------

------解决方案--------------------
AnsiString c = "123456789123456.9";
AnsiString d = "123456789012345.9";

double b = StrToFloat(c.SubString(1,c.Pos(".")-1));
double a = StrToFloat(d.SubString(1,d.Pos(".")-1));

AnsiString e = c.SubString(c.Pos(".")+1,c.Length());
AnsiString f = d.SubString(d.Pos(".")+1,d.Length());
int len = e.Length()>f.Length()?e.Length():f.Length();
if(e.Length()!=f.Length())
{
if(e.Length()>f.Length())
{
for(int i =0;i<e.Length()-f.Length();i++)
{
f += "0";
}
}
else
{
for(int j =0;j<f.Length()-e.Length();j++)
{
e += "0";
}
}
}
AnsiString g = FloatToStr(StrToFloat(e)+StrToFloat(f));
AnsiString result;
if(g.Length()>len)
{
result = FloatToStr(a+b+1)+"."+g.SubString(2,g.Length());
}else
{
result = FloatToStr(a+b)+"."+g.SubString(1,g.Length());
}

ShowMessage(result);