在java Float.parseFloat中舍入 [英] Rounding in java Float.parseFloat

查看:199
本文介绍了在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.

这篇关于在java Float.parseFloat中舍入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆