简体   繁体   English

以2位精度存储双精度

[英]Storing a double with 2 digit precision

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 谢谢,-Mike

"x" stores the number - it doesn't pay attention to the precision. “x”存储数字 - 它不注重精度。

To output the number as a string with a specific amount of precision, use a format specifier , ie: 要将数字作为具有特定精度的字符串输出,请使用格式说明符 ,即:

Console.WriteLine(x.ToString("F2"));

double is an IEEE-754 double-precision floating point number. double是IEEE-754双精度浮点数。 It always has exactly 53 bits of precision. 它总是具有正好53位的精度。 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. 它不具有可以在“小数位”精确表示的精度 - 正如短语暗示的那样,“小数位”仅测量相对于基数为10的数字系统的精度。

If what you want is simply to change the precision the value is displayed with, then you can use one of the ToString() overloads to do that, as other answers here point out. 如果你想要的只是改变显示值的精度,那么你可以使用其中一个ToString()重载来做到这一点,这里的其他答案指出。

Remember too that double is an approximation of a continuous real value; 还要记住, double连续实值的近似值; that is, a value where there is no implied discrete quantization (although the values are in fact quantized to 53 bits). 也就是说,没有隐含离散量化的值(尽管这些值实际上被量化为53位)。 If what you are trying to represent is a discrete value, 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 decimal type. 如果您要表示的是离散值,则存在隐式量化(例如,如果您使用的是货币值,其中0.1和0.099999999999999之间存在显着差异)那么您应该使用数据类型它基于整数语义 - 使用缩放整数的定点表示或类似.NET decimal类型的定义。

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));

The decimal data type has a concept of variable precision. decimal数据类型具有可变精度的概念。 The double data type does not. 双数据类型没有。 Its precision is always 53 bits. 它的精度始终为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. double的默认字符串表示形式略微0.999999999999以最小化奇怪的值,如0.999999999999 ,然后删除任何尾随零。 As other answers note, you can change that behavior by using one of the type's ToString overloads. 正如其他答案所述,您可以使用类型的ToString重载之一来更改该行为。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM