Java Double Multiplication explanation? [duplicate

2019-06-19 00:47发布

This question already has an answer here:

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

I am interested as to why this produces 434.99999999999994 rather than 435.0 . Thanks

标签: java double
1条回答
Animai°情兽
2楼-- · 2019-06-19 01:19

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

If you want exact result, please use BigDecimal.

查看更多
登录 后发表回答