在java Float.parseFloat中舍入

问题描述:

鉴于以下代码,我希望它返回float = 32000.0001。但相反,它返回float = 32000.0。

Given the following code, I would expect it to return "float = 32000.0001". But instead, it returns "float = 32000.0".

System.out.println("float = "+Float.parseFloat("32000.0001"));

我能做些什么来阻止/控制舍入?我希望返回的完整值没有舍入。

Is there something I can do to prevent/control the rounding? I want the full value returned with no rounding.

浮点数只有24位精度,不足以保持您的值中的位数。舍入不是由于解析而是由于数字的大小。如果需要浮点,则必须使用double;如果需要任意精度,则必须使用BigDecimal。

A float has only 24 bits of precision, which is insufficient to hold the number of digits in your value. The rounding is not due to the parse but to the size of the number. You must use a double if you require floating point, or use BigDecimal if you need arbitrary precision.