c# float类型和double类型相乘出现精度丢失
c# float类型和double类型相乘出现精度丢失
double db = 4.0; double db2 = 1.3; float f = 1.3F; float f2 = 4.0F; Decimal de = Convert.ToDecimal(f); MessageBox.Show("double * float " + (db * f).ToString() + "\r\n double * double " + (db * db2).ToString()); MessageBox.Show("double * (double)float " + (db * Convert.ToDouble(f)).ToString() + "\r\n float * float " + (f * f2).ToString() + "\r\n (Decimal)double * (Decimal)float " + (Convert.ToDecimal(db) * Convert.ToDecimal(f)).ToString()); //Convert.ToDecimal(db)转化成Decimal类型计算,Decimal 128位高精度浮点数,常用于金融运算,不会出现浮点数计算的误差
本文解决办法参考http://www.cnblogs.com/quietwalk/archive/2010/05/02/1725885.html