在 C# 中截断双精度值的位数

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

Truncate number of digit of double value in C#

c#

提问by Firoz

How can i truncate the leading digit of double value in C#,I have tried Math.Round(doublevalue,2) but not giving the require result. and i didn't find any other method in Math class.

如何在 C# 中截断 double 值的前导数字,我试过 Math.Round(doublevalue,2) 但没有给出 require 结果。我在数学课上没有找到任何其他方法。

For example i have value 12.123456789 and i only need 12.12.

例如,我的值是 12.123456789,而我只需要 12.12。

采纳答案by Jon Skeet

EDIT: It's been pointed out that these approaches roundthe value instead of truncating. It's hard to genuinely truncate a doublevalue because it's not really in the right base... but truncating a decimalvalue is more feasible.

编辑:有人指出,这些方法是围绕值而不是截断。很难真正截断一个double值,因为它实际上不在正确的基础上……但截断一个decimal值更可行。

You should use an appropriate format string, either customor standard, e.g.

您应该使用适当的格式字符串,自定义标准,例如

string x = d.ToString("0.00");

or

或者

string x = d.ToString("F2");

It's worth being aware that a double value itself doesn't "know" how many decimal places it has. It's only when you convert it to a string that it really makes sense to do so. Using Math.Roundwill get the closest double value to x.xx00000(if you see what I mean) but it almost certainly won't be the exact value x.xx00000due to the way binary floating point types work.

值得注意的是,double 值本身并不“知道”它有多少个小数位。只有当您将其转换为字符串时,这样做才真正有意义。使用Math.Round将获得最接近的 double 值x.xx00000(如果你明白我的意思),但x.xx00000由于二进制浮点类型的工作方式,它几乎肯定不会是确切的值。

If you need this for anything otherthan string formatting, you should consider using decimalinstead. What does the value actually represent?

如果您需要除字符串格式之外的任何其他内容,您应该考虑使用它decimal。值实际上代表什么?

I have articles on binary floating pointand decimal floating pointin .NET which you may find useful.

我有关于.NET 中的二进制浮点十进制浮点的文章,您可能会发现它们很有用。

回答by JayJay

object number = 12.123345534;
string.Format({"0:00"},number.ToString());

对象编号 = 12.123345534;
string.Format({"0:00"},number.ToString());

回答by LukeH

What have you tried? It works as expected for me:

你尝试过什么?它按我的预期工作:

double original = 12.123456789;

double truncated = Math.Truncate(original * 100) / 100;

Console.WriteLine(truncated);    // displays 12.12

回答by Robin Day

This code....

这段代码....

double x = 12.123456789;
Console.WriteLine(x);
x = Math.Round(x, 2);
Console.WriteLine(x);

Returns this....

返回这个....

12.123456789
12.12

What is your desired result that is different?

你想要的结果是什么不同?

If you want to keep the value as a double, and just strip of any digits after the second decimal place and not actually round the number then you can simply subtract 0.005 from your number so that round will then work. For example.

如果您想将该值保留为双精度值,并且只是去掉第二个小数位后的任何数字而不实际舍入该数字,那么您只需从您的数字中减去 0.005,这样该舍入就可以工作。例如。

double x = 98.7654321;
Console.WriteLine(x);
double y = Math.Round(x - 0.005, 2);
Console.WriteLine(y);

Produces this...

产生这个...

98.7654321
98.76

回答by Dan McClain

If you are looking to have two points after the decimal without rounding the number, the following should work

如果您希望小数点后有两个点而不舍入数字,则以下应该有效

string doubleString = doublevalue.ToString("0.0000"); //To ensure we have a sufficiently lengthed string to avoid index issues
Console.Writeline(doubleString
             .Substring(0, (doubleString.IndexOf(".") +1) +2)); 

The second parameter of substring is the count, and IndexOf returns to zero-based index, so we have to add one to that before we add the 2 decimal values.

substring 的第二个参数是计数,IndexOf 返回从零开始的索引,因此我们必须在添加 2 个十进制值之前对其加 1。

This answer is assuming that the value should NOT be rounded

这个答案假设值不应该四舍五入

回答by Dan McClain

I'm sure there's something more .netty out there but why not just:-

我敢肯定还有更多的 .netty 存在,但为什么不只是:-

double truncVal = Math.Truncate(val * 100) / 100;
double remainder = val-truncVal;

回答by JonoW

How about:

怎么样:

double num = 12.12890;
double truncatedNum = ((int)(num * 100))/100.00;

回答by Philippe Leybaert

This could work (although not tested):

这可以工作(虽然没有测试):

public double RoundDown(this double value, int digits)
{
     int factor = Math.Pow(10,digits);

     return Math.Truncate(value * factor) / factor;
}

Then you simply use it like this:

然后你只需像这样使用它:

double rounded = number.RoundDown(2);

回答by Carlos Oliveira

double original = 12.123456789;  

double truncated = Truncate(original, 2);  

Console.WriteLine(truncated.ToString());
// or 
// Console.WriteLine(truncated.ToString("0.00"));
// or 
// Console.WriteLine(Truncate(original, 2).ToString("0.00"));


public static double Truncate(double value, int precision)
{
    return Math.Truncate(value * Math.Pow(10, precision)) / Math.Pow(10, precision);
}

回答by George P

For vb.net use this extension:

对于 vb.net 使用这个扩展:

Imports System.Runtime.CompilerServices

Module DoubleExtensions

    <Extension()>
    Public Function Truncate(dValue As Double, digits As Integer)

        Dim factor As Integer
        factor = Math.Pow(10, digits)

        Return Math.Truncate(dValue * factor) / factor

    End Function
End Module