如题。C#中取小数有效数字采用round函数,遇到0.499995就是0.49,而我需要0.50
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
double x = 0.499995;
textBox1.Text += float.Parse(x.ToString()).ToString("f2");
结果是0.50
decimal Round(decimal d, int decimals, MidpointRounding mode)