以 2 位精度存储双精度值 [英] Storing a double with 2 digit precision
本文介绍了以 2 位精度存储双精度值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何将双精度数转换为 2 位精度?
How do I convert a double to have precision of 2 places?
例如:
double x = 1.00d;
Console.WriteLine(Math.Round(x,2,MidpointRounding.AwayFromZero));
//Should output 1.00 for me, but it outputs just 1.
我正在寻找的是将 1.00 存储在双变量中,当打印到控制台时会打印 1.00.
What I'm looking for is to store 1.00 in a double variable which when printed to console prints 1.00.
谢谢,-迈克
推荐答案
"x" 存储数字 - 它不注意精度.
"x" stores the number - it doesn't pay attention to the precision.
要将数字输出为具有特定精度的字符串,请使用 格式说明符,即:
To output the number as a string with a specific amount of precision, use a format specifier, ie:
Console.WriteLine(x.ToString("F2"));
这篇关于以 2 位精度存储双精度值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文