如题。C#中取小数有效数字采用round函数,遇到0.499995就是0.49,而我需要0.50
3条回答 默认 最新
- threenewbee 2016-03-20 19: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
解决 无用评论 打赏 举报