C# 以 2 位精度存储双精度值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9403491/
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
Storing a double with 2 digit precision
提问by Mike
How do I convert a double to have precision of 2 places?
如何将双精度数转换为 2 位精度?
For ex:
例如:
double x = 1.00d;
Console.WriteLine(Math.Round(x,2,MidpointRounding.AwayFromZero));
//Should output 1.00 for me, but it outputs just 1.
What I'm looking for is to store 1.00 in a double variable which when printed to console prints 1.00.
我正在寻找的是将 1.00 存储在一个双变量中,当打印到控制台时会打印 1.00。
Thanks, -Mike
谢谢,-迈克
采纳答案by Reed Copsey
"x" stores the number - it doesn't pay attention to the precision.
“x”存储数字 - 它不注意精度。
To output the number as a stringwith a specific amount of precision, use a format specifier, ie:
要将数字输出为具有特定精度的字符串,请使用格式说明符,即:
Console.WriteLine(x.ToString("F2"));
回答by dasblinkenlight
This is not a question of storing, it is a question of formatting.
这不是存储问题,而是格式问题。
This code produces 1.00:
此代码产生1.00:
double x = 1.00d;
Console.WriteLine("{0:0.00}", Math.Round(x, 2, MidpointRounding.AwayFromZero));
回答by Daniel Pryden
doubleis an IEEE-754 double-precision floating point number. It always has exactly 53 bits of precision. It does not have a precision that can be exactly represented in "decimal places" -- as the phrase implies, "decimal places" only measures precision relative to the base-10 number system.
double是 IEEE-754 双精度浮点数。它总是精确到 53 位精度。它没有可以用“小数位”精确表示的精度——正如这句话所暗示的,“小数位”仅测量相对于基数 10 数字系统的精度。
If what you want is simply to change the precision the value is displayedwith, then you can use one of the ToString()overloads to do that, as other answers here point out.
如果您想要的只是更改显示值的精度,那么您可以使用ToString()重载之一来做到这一点,正如此处的其他答案所指出的那样。
Remember too that doubleis an approximation of a continuousreal value; that is, a value where there is no implied discrete quantization (although the values are in fact quantized to 53 bits). If what you are trying to represent is a discretevalue, where there is an implicit quantization (for example, if you are working with currency values, where there is a significant difference between 0.1 and 0.099999999999999) then you should probably be using a data type that is based on integer semantics -- either a fixed-point representation using a scaled integer or something like the .NET decimaltype.
还要记住,这double是一个连续实数值的近似值;也就是说,一个没有隐含离散量化的值(尽管这些值实际上被量化为 53 位)。如果您尝试表示的是离散值,其中存在隐式量化(例如,如果您正在处理货币值,则 0.1 和 0.0999999999999999 之间存在显着差异),那么您可能应该使用数据类型它基于整数语义——使用缩放整数或类似 .NETdecimal类型的定点表示。
回答by phoog
The decimaldata type has a concept of variable precision. The double data type does not. Its precision is always 53 bits.
该decimal数据类型具有的变量精度的概念。double 数据类型没有。它的精度始终为 53 位。
The default string representation of a double rounds it slightly to minimize odd-looking values like 0.999999999999, and then drops any trailing zeros. As other answers note, you can change that behavior by using one of the type's ToStringoverloads.
double 的默认字符串表示将其稍微四舍五入以最小化看起来像 的奇怪值0.999999999999,然后删除任何尾随零。正如其他答案所指出的,您可以通过使用类型的ToString重载之一来更改该行为。

