将双精度数字舍入为 3 位有效数字
Does anybody know how I can round a double value to 3 significant figures like the examples on this website
http://www.purplemath.com/modules/rounding2.htm
Note: significant figures are not the same as decimal places
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
如果您想手动执行此操作:
上述方法将双精度数舍入为所需的有效数字数,处理负数,并且可以明确告知向上或向下舍入
If you want to do it by hand:
The above method rounds a double to a desired number of significant figures, handles negative numbers, and can be explicitly told to round up or down
Sean Owen (https://stackoverflow.com/a/7548871/274677) 给出的答案没有任何问题。但是,根据您的用例,您可能希望获得字符串表示形式。在这种情况下,IMO 最好在仍在 BigDecimal 空间中时使用以下方法进行转换:
... 如果这是您的用例,那么您可能会感到沮丧,根据 Owen 的答案改编的代码将产生以下结果:
... 而不是严格的 more准确
0.990
。如果这些事情在您的用例中很重要,那么我建议对欧文的答案进行以下调整;显然,您也可以返回 BigDecimal 本身,而不是调用 toPlainString() - 我只是为了完整性而提供这种方式。
There's nothing wrong with the answer given by Sean Owen (https://stackoverflow.com/a/7548871/274677). However depending on your use case you might want to arrive at a String representation. In that case, IMO it is best to convert while still in BigDecimal space using:
... if that's your use case then you might be frustrated that the code adapted from Owen's answer will produce the following:
... instead of the strictly more accurate
0.990
.If such things are important in your use case then I suggest the following adaptation to Owen's answer ; you can obviously also return the BigDecimal itself instead of calling
toPlainString()
— I just provide it this way for completeness.双 d=3.142568;
System.out.printf("答案:%.3f", d);
double d=3.142568;
System.out.printf("Answer : %.3f", d);
你不能。双精度数以二进制表示。它们没有要四舍五入的小数位。获得特定小数位数的唯一方法是将其转换为小数基数并将其保留在那里。当您将其转换回双精度时,您再次失去了小数精度。
对于这里和其他地方的所有转换为其他基数和反向转换,或乘以和除以 10 的幂的粉丝,请显示结果双精度值 % 0.001 或所需精度规定的任何内容,并解释结果。
编辑:具体来说,这些技术的支持者需要解释以下代码的 92% 失败率:
You can't. Doubles are representing in binary. They do not have decimal places to be rounded to. The only way you can get a specific number of decimal places is to convert it to a decimal radix and leave it there. The moment you convert it back to double you have lost the decimal precision again.
For all the fans, here and elsewhere, of converting to other radixes and back, or multiplying and dividing by powers of ten, please display the resulting double value % 0.001 or whatever the required precision dictates, and explain the result.
EDIT: Specifically, the proponents of those techniques need to explain the 92% failure rate of the following code:
我通常不会舍入数字本身,而是在需要显示数字时舍入数字的字符串表示形式,因为通常重要的是显示,需要舍入(尽管在情况,也许是你的情况,但如果是这样,你需要详细说明)。这样,我的号码就可以保持其准确性,但它的显示会被简化且更易于阅读。为此,可以使用 DecimalFormat 对象,例如用“0.000”字符串初始化 (
new DecimalFormat("0.000")
),或使用String.format("%.3f" , myDouble)
,或其他几种方式。例如:
I usually don't round the number itself but round the String representation of the number when I need to display it because usually it's the display that matters, that needs the rounding (although this may not be true in situations, and perhaps yours, but you need to elaborate on this if so). This way, my number retains its accuracy, but it's display is simplified and easier to read. To do this, one can use a DecimalFormat object, say initialzed with a "0.000" String (
new DecimalFormat("0.000")
), or useString.format("%.3f", myDouble)
, or several other ways.For example: