Java parseFloat() 与 parseDouble()
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22970240/
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
parseFloat() vs. parseDouble()
提问by vikifor
I am trying the code below to convert string to float and double but getting different results.
我正在尝试使用下面的代码将字符串转换为 float 和 double 但得到不同的结果。
Code:
代码:
System.out.println(Float.parseFloat("120059389"));
System.out.println(Double.parseDouble("120059389"));
Output:
输出:
1.20059392E8
1.20059389E8
Could somebody explain me why I got different result for parsing string in float and double? What are the ranges for float and double?
有人能解释一下为什么我在 float 和 double 中解析字符串时得到不同的结果吗?float 和 double 的范围是多少?
回答by Farmer Joe
The difference lies in the fact that Double
and Float
store numbers differently.
不同之处在于Double
和Float
存储数字不同。
With Single and Double precision, allowing Double
to give Doublethe amount of precision the Float
can handle.
使用单精度和双精度,允许Double
给双精度Float
可以处理的量。
So the simple answer is that because of Float
's limited memory in comparison to Double
information is lost upon conversion of numbers out of the range Float
can handle.
所以简单的答案是,由于Float
与Double
信息相比的有限内存在转换超出Float
可以处理的范围的数字时丢失。
Float
: 32-bit NumbersDouble
: 64-bit Numbers
Float
: 32 位数字Double
: 64 位数字
So in the conversion some information is lost when converting to the Float
because it is truncated.
所以在转换中,一些信息在转换为 时会丢失,Float
因为它被截断了。
Generally...
一般来说...
Float
stores numbers as 1 bit for the sign (-/+
), 8 bits for the Exponent, and 23 bits for the fraction.
Float
将数字存储为符号 ( -/+
) 的 1 位、指数的 8 位和分数的 23 位。
Double
stores numbers as 1 bit for the sign (-/+
), 8 bits for the Exponent, and 53 bits for the fraction.
Double
将数字存储为符号 ( -/+
) 的 1 位、指数的 8 位和分数的 53 位。
When you convert your number 120059389 = 111001001111111010111111101b
has 27 bits worth of information which can be covered by the Double
's 53 bit allowence, but not by the Float
's 23 bit allowance, and the data is truncated at the least significant end.
当您转换您的数字时,您的数字120059389 = 111001001111111010111111101b
有 27 位的信息,这些信息可以被Double
53 位允许覆盖,但不能被Float
23 位允许覆盖,并且数据在最低有效端被截断。
The conversion will round the number to the nearest representable number using 23 bits 1.20059392 = 111001001111111011000000000b
and the exponent will handle the rest of the expansion.
转换将使用 23 位将数字四舍五入到最接近的可表示数字1.20059392 = 111001001111111011000000000b
,指数将处理其余的扩展。
回答by rgettman
This is because you're trying to parse a float
by giving it more digits of precision than it can handle. The "ulp" (unit in last place) of a float
that big is 8.0
, but the "ulp" for a double
that big is still reasonably small. That is, at that magnitude, the closest possible float
values differ by 8
, but the closest double
values, with more precision, differ by far less.
这是因为您试图float
通过为其提供比它可以处理的更多位的精度来解析 a 。那个大的“ulp”(最后一个单位)float
是8.0
,但是那个大的“ulp”double
仍然相当小。也就是说,在那个量级,最接近的可能float
值相差8
,但最接近的double
值,精度更高,相差小得多。
System.out.println(Math.ulp(120059389f));
System.out.println(Math.ulp(120059389d));
This prints:
这打印:
8.0
1.4901161193847656E-8
So the Float
parser must use the closest float
to the true value of 120059389
, which happens to be 1.20059392E8
.
所以Float
解析器必须使用最接近float
的真实值120059389
,也就是1.20059392E8
.
回答by CramerTV
The earlier links and answers give good technical answers. The 'laymans' answer is that a float is 32 bits and a double is 64 bits. Some of those bits are used for the number and some are used for the exponent. The number you put in your code simply had too many digits for the 32 bit 'float'. The 64 bit 'double' has more bits and can be more precise with larger numbers.
较早的链接和答案提供了很好的技术答案。“外行”的答案是浮点数为 32 位,双精度数为 64 位。这些位中的一些用于数字,一些用于指数。您在代码中输入的数字对于 32 位“浮点数”来说数字太多。64 位“double”具有更多位,并且可以使用更大的数字更精确。
The same concept holds for even larger numbers when you reach the limits of a 64 bit double and need 128 bits of precision.
当您达到 64 位双精度数的限制并需要 128 位精度时,同样的概念适用于更大的数字。