2 zwx649294308 zwx649294308 于 2016.03.21 00:09 提问

C#中取小数有效数字采用round函数,遇到0.499995就是0.49,而我需要0.50
c#

如题。C#中取小数有效数字采用round函数,遇到0.499995就是0.49,而我需要0.50

3个回答

caozhy
caozhy   Ds   Rxr 2016.03.21 03:44
 using System;

public class Test
{
    public static double myround(double d)
    {
        return (int)(d * 100 + 0.5) / 100.0;
    }
    public static void Main()
    {
        // your code goes here
        double d1 = 0.499995;
        double d2 = 0.494;
        double d3 = 0.495;
        double d4 = 0.496;
        Console.WriteLine("{0:0.00} {1:0.00} {2:0.00} {3:0.00}", myround(d1), myround(d2), myround(d3), myround(d4));
    }
}

0.50 0.49 0.50 0.50

herozhangbz
herozhangbz   2016.03.21 11:31
             double x = 0.499995;
            textBox1.Text += float.Parse(x.ToString()).ToString("f2");

结果是0.50

u012494939
u012494939   2016.03.21 11:55

decimal Round(decimal d, int decimals, MidpointRounding mode)

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!