如下代码,得到的结果是0.069999999998;
请问如果才能得到0.07呢?
double temp1 = 0, temp2 = 0;
temp1 = 4.38; temp2 = 4.52;
double temp4 = (temp1 - temp2) / 2;
MessageBox.Show(temp4.ToString());
请问如果才能得到0.07呢?
double temp1 = 0, temp2 = 0;
temp1 = 4.38; temp2 = 4.52;
double temp4 = (temp1 - temp2) / 2;
MessageBox.Show(temp4.ToString());
double temp1 = 0, temp2 = 0;
temp1 = 4.38; temp2 = 4.52;
double temp4 = (temp1 - temp2) / 2;
MessageBox.Show(temp4.ToString("F2"));
temp1 = 4.38; temp2 = 4.52;
decimal temp4 = (temp1 - temp2) / 2;
MessageBox.Show(temp4.ToString());
temp1 = 4.38M;
temp2 = 4.52M;
decimal temp4 = (temp1 - temp2) / 2;
MessageBox.Show(temp4.ToString());
什么意思?是要保留3位或4位么?
3位就是F3,4位就是F4
F后面的数字就是小数的位数
目前我的程序都是用的double型的数据,但最后显示结果的时候才发现都存在这样的误差,那我该怎么消除这样的误差呢?难道只能 Math.Round(temp4 * 10000) / 10000)么?
我计算的结果不能预测小数点后有几位,只需要按实际情况就可,
例如0.7就保留1位,
如果结果是0.56,那就保留2位
并不是看到是多少,存的就是多少.看空军 11楼的,如果要绝对不变的精确值,替换成 decimal
感觉不应该存在0.7=0.6999999的bug啊//
double j =0.7 ;i==j再来
double i = 0.6999999999999999*10;
i = 6.9999999999999982double i = 0.6999999999999999*100;
i = 69.999999999999986double i = 0.6999999999999999*1000;
i = 699.99999999999989这样够直观了吧?
保留两位
MessageBox.Show(temp4.ToString("0.000"));
保留三位
MessageBox.Show(Math.Round(temp4, 2).ToString());
//保留三位
MessageBox.Show(Math.Round(temp4, 3).ToString());
int X = Convert.toInt32(10, intN);
int X2 = Convert.toInt32(dubA * intX);
dubB = Convert.toDouble(X2)/ X;