float ft = (float) Math.Round(fdata / inc)* inc;
其中fdata 为要转化的数,inc为数据精度。
上面的写法对吗?经常有数据出错!

解决方案 »

  1.   

    public static double Math.Round(
    double value,
    int digits
    )参数value
    类型:System.Double
    要舍入的双精度浮点数。digits
    类型:System.Int32
    返回值中的小数位数(精度)。返回值类型:System.Double
    精度等于 digits,最接近 value 的数字。
      

  2.   

    例如:
    float fdata = 3.141595265;
    float ft = (float)Math.Round(fdata, 2); // 保留小数点后2位
      

  3.   

    Round函数首先用的就不对了,1楼的不错
      

  4.   

    public static double Round (
    double a
    )
    将双精度浮点值舍入为最接近的整数。 
    有这样的方法,我的函数好象没错!
    当fdata =0.98817,inc=0.0001时ft =0.9881999,不知道什么原因?
      

  5.   

    public static double Round ( 
    double a 
      

  6.   


    因为浮点数计算可能损失精度。应该这样用:flaot fdata = 0.98817;
    int inc = 4;
    float ft = (float)System.Math.Round(fdata, inc);