数值位数问题,请进来看一下
一组double型的数据(有正有负),现在想只把这组数据的小数位都只保留2位,怎么实现啊? 问题点数:0、回复次数:14Top
1 楼jishiping(JSP 季世平)回复于 2004-09-02 22:58:26 得分 0
如果只是想显示出来是2位的话,用格式化输出就可以了。
如果真的是想改数据的话,那么
double df = xxx;
df = ((abs(df*1000)+5)/10)/100.0*(df<0?-1:1);Top
2 楼zxcdewq(zxcdewq)回复于 2004-09-03 08:55:26 得分 0
double d = -12.34567;
AnsiString str;
char ch[20];
sprintf(ch, "%0.2lf", d);Top
3 楼gigilee(gigilee)回复于 2004-09-03 09:48:05 得分 0
我也想知道
帮你顶
比如像vb中的format函数?Top
4 楼yuxuan1120(予萱)回复于 2004-09-03 10:01:11 得分 0
如jishiping所说去做的话
比如说12.345678,按照你的方法最后是12.350678
也没有什么改变啊,该数还是double型的啊
我不是要输出数据,而是将整组数据改为小数点后只有2位的
请大家帮帮忙,看怎么才能实现,谢谢
Top
5 楼PPower(月亮光光,照地堂)回复于 2004-09-03 10:14:27 得分 0
double A = 20.34763476 ;
A = int(A*100 )/100.0 ;Top
6 楼jiangchun_xn(GrayMemory)(再回头·灯火依旧·人不见·潸然泪下)回复于 2004-09-03 11:47:53 得分 0
数据改为小数点后只有2位的
------
这种做法没有意义Top
7 楼cnrealboy(中国男人)回复于 2004-09-03 16:42:59 得分 0
double dA = 20.34763476 ;
//下面是做到4舍5入的方法。
AnsiString strA = FloatToStr(dA);
int iB = strA.Pos(".");
if(iB > 0)//保证有小数
{
if(strA.Length() > (iB + 2)) //保证3位小数才需要处理
{
dA = strA.SubString(1,iB + 2).ToDouble();
AnsiString strC = strA.SubString(iB+3,1);
if(strC.ToInt() >= 5) dA += 0.01;
}
}
Top
8 楼Nomad_man(流浪的人)回复于 2004-09-03 16:54:10 得分 0
先将数据用“.”Split后,用原来的数据减去“.”前面的数据,
就得到小数部分,再格式输出!Top
9 楼Turntogo(星光伴月)回复于 2004-09-03 17:55:38 得分 0
FloatToStrF(Value, Format, Precision, Digits);
用法如下:
float fNum;
AnsiString sNum=FloatToStrF(fNum,ffGeneral,5,2);
下面是摘自BCB帮助的说明文字:
Format Defines
ffGeneral General number format. The value is converted to the shortest possible decimal string using fixed or scientific format. Trailing zeros are removed from the resulting string, and a decimal point appears only if necessary. The resulting string uses fixed point format if the number of digits to the left of the decimal point in the value is less than or equal to the specified precision, and if the value is greater than or equal to 0.00001. Otherwise the resulting string uses scientific format, and the Digits parameter specifies the minimum number of digits in the exponent (between 0 and 4).
ffExponent Scientific format. The value is converted to a string of the form "-d.ddd...E+dddd". The resulting string starts with a minus sign if the number is negative, and one digit always precedes the decimal point. The total number of digits before the exponent in the resulting string (including the one before the decimal point) is given by the Precision parameter. The "E" exponent character in the resulting string is always followed by a plus or minus sign and up to four digits. The Digits parameter specifies the minimum number of digits in the exponent (between 0 and 4).
ffFixed Fixed point format. The value is converted to a string of the form "-ddd.ddd...". The resulting string starts with a minus sign if the number is negative, and at least one digit always precedes the decimal point. The number of digits after the decimal point is given by the Digits parameter--it must be between 0 and 18. If the number of digits to the left of the decimal point is greater than the specified precision, the resulting value will use scientific format.
ffNumber Number format. The value is converted to a string of the form "-d,ddd,ddd.ddd...". The ffNumber format corresponds to the ffFixed format, except that the resulting string contains thousand separators.
ffCurrency Currency format. The value is converted to a string that represents a currency amount. The conversion is controlled by the CurrencyString, CurrencyFormat, NegCurrFormat, ThousandSeparator, and DecimalSeparator global variables, all of which are initialized from the Currency Format in the International section of the Windows Control Panel. The number of digits after the decimal point is given by the Digits parameter--it must be between 0 and 18.Top
10 楼kmfangxun()回复于 2004-09-03 18:21:15 得分 0
double ss=1234234.897698769867;
FormatFloat("##########0.00",ss);Top
11 楼tiegerium(/*唐秀观*/)回复于 2004-09-03 21:04:57 得分 0
C++的double,float有误差,用TCurrency吧。Top
12 楼kwokwinglau(每天前进1%)回复于 2004-09-04 08:20:10 得分 0
double y=123.23568797;
Sh1.PG("Cells",iRow+5+i,iColumn+j*3+1).PS("Value",FormatFloat("0.00",y).c_str());
Top
13 楼tiegerium(/*唐秀观*/)回复于 2004-09-04 09:38:37 得分 0
Turntogo(志在四方)说:
FloatToStrF(Value, Format, Precision, Digits);
用法如下:
float fNum;
AnsiString sNum=FloatToStrF(fNum,ffGeneral,5,2);
double d;
d=-3.5565;
String s=FloatToStrF(d,ffGeneral,3,5);
ShowMessage(s);
//s=-3.56Top
14 楼yuxuan1120(予萱)回复于 2004-09-06 10:27:26 得分 0
sorry,大家
我已经想出怎么做了
double A=1.2345678;
A=(int(A*100))*0.01;
就ok了Top




