【问题标题】:Rounding a variable to two decimal places C# [duplicate]将变量四舍五入到小数点后两位 C# [重复]
【发布时间】:2012-09-19 06:06:12
【问题描述】:

我对如何将变量四舍五入到小数点后两位很感兴趣。在下面的示例中,奖金通常是一个小数点后四位的数字。有什么办法可以确保薪酬变量总是四舍五入到小数点后两位?

  pay = 200 + bonus;
  Console.WriteLine(pay);

【问题讨论】:

标签: c# variables decimal rounding


【解决方案1】:

使用Math.Round 并指定小数位数。

Math.Round(pay,2);

Math.Round Method (Double, Int32)

双精度浮点值四舍五入为指定数字 小数位数。

Math.Round Method (Decimal, Int32)

将十进制值四舍五入为指定的小数位数。

【讨论】:

  • 并确保您使用的是 floatdoubledecimal 类型,否则这将永远无法工作。
  • 我正在使用双精度,但它仍然不适合我。
  • pay = Math.Round(pay, 2);一样称呼它
  • 重要:这会将 0.005 舍入到 0.00!请参阅下面 Jon 的答案中的原因,这应该是最佳答案。
【解决方案2】:
decimal pay  = 1.994444M;

Math.Round(pay , 2); 

【讨论】:

    【解决方案3】:
    Console.WriteLine(decimal.Round(pay,2));
    

    【讨论】:

      【解决方案4】:

      您可以对结果进行四舍五入并使用string.Format 设置精度,如下所示:

      decimal pay = 200.5555m;
      pay = Math.Round(pay + bonus, 2);
      string payAsString = string.Format("{0:0.00}", pay);
      

      【讨论】:

      • 我怀疑你可以在第一行以两种不同的方式使用pay!当您已经使用Math.Round 时,是否也不需要使用字符串格式?如果您不想对实际的数字结构进行四舍五入,而只想缩短生成的字符串,那么字符串格式化是一件好事。在这种情况下,还要考虑"F""F2"(和"N""N2")格式化字符串,例如string.Format("{0:N}", pay) 或等效的pay.ToString("N")
      • @JeppeStigNielsen 您对第一行的看法绝对正确!修正。有趣的是,我以前从未使用过{0:N},我想这是习惯的产物;o)
      • 为了进一步解释,"F" 舍入到由当前线程的当前区域性的NumberFormat 指定的小数位数。这通常是两位小数。 "F2" 总是四舍五入到小数点后两位。 "N""N2" 相似,但它们也根据文化插入组分隔符。例如(11223344.5566m).ToString("N") 可能会在某些文化中产生格式化数字"11,223,344.56"。见Standard Numeric Format Strings
      【解决方案5】:

      使用 System.Math.Round 将十进制值四舍五入为指定的小数位数。

      var pay = 200 + bonus;
      pay = System.Math.Round(pay, 2);
      Console.WriteLine(pay);
      

      MSDN 参考资料:

      【讨论】:

        【解决方案6】:

        确保您提供一个数字,通常使用双精度数。 Math.Round 可以接受 1-3 个参数,第一个参数是要舍入的变量,第二个是小数位数,第三个是舍入类型。

        double pay = 200 + bonus;
        double pay = Math.Round(pay);
        // Rounds to nearest even number, rounding 0.5 will round "down" to zero because zero is even
        double pay = Math.Round(pay, 2, MidpointRounding.ToEven);
        // Rounds up to nearest number
        double pay = Math.Round(pay, 2, MidpointRounding.AwayFromZero);
        

        【讨论】:

          【解决方案7】:

          您应该使用Math.Round 的形式。请注意,Math.Round 默认为庄家四舍五入(四舍五入到最接近的偶数),除非您指定 MidpointRounding 值。如果你不想使用银行家四舍五入,你应该使用Math.Round(decimal d, int decimals, MidpointRounding mode),像这样:

          Math.Round(pay, 2, MidpointRounding.AwayFromZero); // .005 rounds up to 0.01
          Math.Round(pay, 2, MidpointRounding.ToEven);       // .005 rounds to nearest even (0.00) 
          Math.Round(pay, 2);    // Defaults to MidpointRounding.ToEven
          

          (Why does .NET use banker's rounding?)

          【讨论】:

            【解决方案8】:

            注意Round rounds

            所以(我不知道这对你的行业是否重要),但是:

            float a = 12.345f;
            Math.Round(a,2);
            
            //result:12,35, and NOT 12.34 !
            

            为了让 您的情况更准确,我们可以这样做:

            int aInt = (int)(a*100);
            float aFloat= aInt /100.0f;
            //result:12,34 
            

            【讨论】:

            • Math.Round 不接受浮点数作为参数。
            猜你喜欢
            • 1970-01-01
            • 2019-01-20
            • 1970-01-01
            • 1970-01-01
            • 2013-03-23
            • 1970-01-01
            • 1970-01-01
            • 2022-01-02
            • 1970-01-01
            相关资源
            最近更新 更多