java中双精度乘法的精度?
java中双精度值的乘法运算符的保证精度是多少?
例如,2.2 * 100 是 220.00000000000003,但 220 是双精度数。 220.00000000000003 是 220 之后的下一个双精度值。
What is the guaranteed accuracy of multiplication operator for double values in java?
For example, 2.2 * 100 is 220.00000000000003, but 220 is a double number. 220.00000000000003 is the next double after 220.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
乘法工作正常,但
2.2
不能精确地表示为双精度。最接近的双精度数是:某些软件会将后一个值打印为
2.2
,但这并不意味着它是准确的。这只是意味着它被视为“足够接近”。The multiplication is working fine, but
2.2
cannot be represented exactly as a double. The closest doubles are:Some software will print the latter value as
2.2
, but that doesn't mean it's exact. It just means it's treated as "close enough".如果您正在处理财务数据,请不要使用
float或double
,只需使用java.math.BigDecimal
If you are working with financials data dont use
float or double
just usejava.math.BigDecimal
您可以使用 BigDecimal,但请确保使用 BigDecimal(String) 的构造,而不是 BigDecimal(double)。它们之间的区别是:
you can use BigDecimal, but make sure that you use the construct of BigDecimal(String) rather than BigDecimal(double). The difference between them is: