zwx649294308 2016-03-20 16:09 采纳率: 0%
浏览 1776

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

如题。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

    评论

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料