在 C# 中四舍五入到小数点后两位

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13522095/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 08:53:27  来源:igfitidea点击:

Rounding down to 2 decimal places in c#

c#decimalrounding

提问by startupsmith

How can I multiply two decimals and round the result down to 2 decimal places?

如何乘以两位小数并将结果四舍五入到小数点后两位?

For example if the equation is 41.75 x 0.1 the result will be 4.175. If I do this in c# with decimals it will automatically round up to 4.18. I would like to round down to 4.17.

例如,如果方程为 41.75 x 0.1,结果将为 4.175。如果我在 c# 中使用小数执行此操作,它将自动四舍五入到 4.18。我想四舍五入到 4.17。

I tried using Math.Floor but it just rounds down to 4.00. Here is an example:

我尝试使用 Math.Floor 但它只是四舍五入到 4.00。下面是一个例子:

Math.Floor (41.75 * 0.1);

采纳答案by Aren

The Math.Round(...)function has an Enum to tell it what rounding strategy to use. Unfortunately the two defined won't exactly fit your situation.

Math.Round(...)函数有一个 Enum 来告诉它使用什么舍入策略。不幸的是,这两个定义并不完全适合您的情况。

The two Midpoint Rounding modes are:

两种中点舍入模式是:

  1. AwayFromZero- When a number is halfway between two others, it is rounded toward the nearest number that is away from zero. (Aka, round up)
  2. ToEven- When a number is halfway between two others, it is rounded toward the nearest even number. (Will Favor .16 over .17, and .18 over .17)
  1. AwayFromZero- 当一个数字在其他两个数字的中间时,它会向离零最近的数字四舍五入。(又名,四舍五入)
  2. ToEven- 当一个数字介于其他两个数字之间时,它会向最接近的偶数四舍五入。(比 0.17 更喜欢 0.16,比 0.17 更喜欢 0.18)

What you want to use is Floorwith some multiplication.

你想要使用的是Floor一些乘法。

var output = Math.Floor((41.75 * 0.1) * 100) / 100;

The outputvariable should have 4.17 in it now.

output变量应该有它的4.17了。

In fact you can also write a function to take a variable length as well:

事实上,您也可以编写一个函数来获取可变长度:

public decimal RoundDown(decimal i, double decimalPlaces)
{
   var power = Convert.ToDecimal(Math.Pow(10, decimalPlaces));
   return Math.Floor(i * power) / power;
}

回答by Kami

There is no native support for precision floor/ceillin in c#.

C# 中没有对精确地板/天花板的原生支持。

You can however mimic the functionality by multiplying the number, the floor, and then divide by the same multiplier.

但是,您可以通过乘以数字、下限,然后除以相同的乘数来模拟功能。

eg,

例如,

decimal y = 4.314M;
decimal x = Math.Floor(y * 100) / 100;  // To two decimal places (use 1000 for 3 etc)
Console.WriteLine(x);  // 4.31

Not the ideal solution, but should work if the number is small.

不是理想的解决方案,但如果数量很少,应该可以工作。

回答by SLenik

One more solution is to make rounding toward zero from rounding away from zero. It should be something like this:

另一种解决方案是从远离零舍入到零。它应该是这样的:

    static decimal DecimalTowardZero(decimal value, int decimals)
    {
        // rounding away from zero
        var rounded = decimal.Round(value, decimals, MidpointRounding.AwayFromZero);

        // if the absolute rounded result is greater 
        // than the absolute source number we need to correct result
        if (Math.Abs(rounded) > Math.Abs(value))
        {
            return rounded - new decimal(1, 0, 0, value < 0, (byte)decimals);
        }
        else
        {
            return rounded;
        }
    }

回答by Wichukorn Dandecha

public double RoundDown(double number, int decimalPlaces)
{
     return Math.Floor(number * Math.Pow(10, decimalPlaces)) / Math.Pow(10, decimalPlaces);
}