java Java双乘解释?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17571907/
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
Java Double Multiplication explanation?
提问by NightStrider
I recently tested something out that I heard using the following code
我最近使用以下代码测试了我听到的一些东西
public static void main(String[] args) {
double x = 4.35 * 100;
System.out.println(x);
}
.
public static void main(String[] args) {
double x = 4.35 * 100;
System.out.println(x);
}
.
I am interested as to why this produces 434.99999999999994 rather than 435.0 . Thanks
我很感兴趣,为什么这会产生 434.99999999999994 而不是 435.0 。谢谢
回答by Arnaud Denoyelle
when you type
当你打字时
double x = 4.35;
x is not stored as-is. It is stored in a approaching form (probably 4.349999999 in this case).
x 未按原样存储。它以接近的形式存储(在这种情况下可能是 4.349999999)。
If you want exact result, please use BigDecimal.
如果您想要确切的结果,请使用BigDecimal。