C# 获取不舍入的最后 2 个小数位

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9931795/
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-09 11:16:16  来源:igfitidea点击:

Get Last 2 Decimal Places with No Rounding

c#roundingdecimal

提问by mint

In C#, I'm trying to get the last two decimal places of a double with NO rounding. I've tried everything from Math.Floorto Math.Truncateand nothing is working.

在 C# 中,我试图在没有四舍五入的情况下获得双精度的最后两位小数。我已经尝试了从Math.Floor到的所有方法Math.Truncate,但没有任何效果。

Samples of the results I'd like:

我想要的结果样本:

1,424.2488298 -> 1,424.24
53.5821 -> 53.58
10,209.2991 -> 10,209.29

Any ideas?

有任何想法吗?

采纳答案by Ed S.

Well, mathematically it's simple:

好吧,从数学上讲,这很简单:

var f = 1.1234;
f = Math.Truncate(f * 100) / 100;  // f == 1.12

Move the decimal two places to the right, cast to an int to truncate, shift it back to the left two places. There may be ways in the framework to do it too, but I can't look right now. You could generalize it:

将小数点向右移动两位,转换为 int 进行截断,将其移回左边两位。框架中可能也有方法可以做到这一点,但我现在看不到。你可以概括一下:

double Truncate(double value, int places)
{
    // not sure if you care to handle negative numbers...       
    var f = Math.Pow( 10, places );
    return Math.Truncate( value * f ) / f;
}

回答by ionden

 double d = Math.Truncate(d * 100) / 100;

回答by ionden

A general solution:

一个通用的解决方案:

    public static double SignificantTruncate(double num, int significantDigits)
    {
        double y = Math.Pow(10, significantDigits);
        return Math.Truncate(num * y) / y;
    }

Then

然后

    double x = 5.3456;
    x = SignificantTruncate(x,2);

Will produce the desired result x=5.34.

会产生想要的结果x=5.34

回答by Eric Lippert

My advice: stop using doublein the first place. If you need decimal rounding then odds are good you should be using decimal. What is your application?

我的建议是:停止使用double摆在首位。如果您需要小数四舍五入,那么您应该使用decimal. 你的应用是什么?

If you do have a double, you can do it like this:

如果你有一个双人,你可以这样做:

double r = whatever;
decimal d = (decimal)r;
decimal truncated = decimal.Truncate(d * 100m) / 100m;

Note that this technique will fail if the absolute value of the double is larger than 792281625142643375935439504, because the multiplication by 100 will fail. If you need to handle values that large then you'll need to use special techniques. (Of course, by the time a double is that large, you are well beyond its ability to represent values with two digits after the decimal place anyway.)

请注意,如果 double 的绝对值大于 792281625142643375935439504,则此技术将失败,因为乘以 100 将失败。如果您需要处理这么大的值,则需要使用特殊技术。(当然,当 double 变得那么大时,无论如何,您都无法用小数点后两位数字来表示值。)

回答by Octane

Math.Round(NumberToRound - (double)0.005,2)

i.e

IE

Math.Round(53.5821 - (double)0.005,2) // 53.58
Math.Round(53.5899 - (double)0.005,2) // 53.58
Math.Round(53.5800 - (double)0.005,2) // 53.58