我定义了一个类 class1,
里面有两个字段 float num1, 
                          float num2实例化类class1后,给两个字段赋值 num1=1024.88    
                                                        num2=3000 (两个字段赋值是从数据库读取的)然后计算 num1 - num2 * 0.2 , 也就是1024.88-3000*0.2 结果却不是两位小数,而是424.8800000002343。
由于是给财务的数据,一分钱都能不错,请问这个怎么解决。多谢 

解决方案 »

  1.   

    math.ceilling
    math.floor
    进舍位,保证分斤舍两不吃亏.
      

  2.   

    浮点数是不会精确的
    你可以保留几位 类似:String.Format("{0:0.00}", 123.4567);  //123.46
      

  3.   

    对 用Math 数学函数 
      

  4.   

    貌似另一个板块我回复了
    “帅锅误差无法避免用decimal 定义 不要用 float/double”
      

  5.   

    float 与计算机中数值表示方法有关,不是准确数据
    使用double
    ToString("0.00");  
      

  6.   

    用math的方法或者tostring()自定义输出格式
      

  7.   

    The Decimal value type represents decimal numbers ranging from positive 79,228,162,514,264,337,593,543,950,335 to negative 79,228,162,514,264,337,593,543,950,335. The Decimal value type is appropriate for financial calculations requiring large numbers of significant integral and fractional digits and no round-off errors. The Decimal type does not eliminate the need for rounding. Rather, it minimizes errors due to rounding. For example, the following code produces a result of 0.9999999999999999999999999999 rather than 1.
    金融计算 请用 decimal