Java如何将浮点数转换为字符串 [英] how does Java convert floats to strings

查看:988
本文介绍了Java如何将浮点数转换为字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  float p = 1.15f; 
BigDecimal bdp = new BigDecimal(p);
float q = 1.1499999f;
float r = 1.14999999f;

System.out.println(p); //1.15
System.out.println(bdp); //1.14999997615814208984375
System.out.println(q); //1.1499999
System.out.println(r); //1.15

所以我明白p1.15的十进制值不能准确表示在二进制。

所以大的小数bdp输出对我来说是完全有意义的...这是浮点数的实际值。

问题1

当浮点p被转换回输出字符串(如1.15)时,这个四舍五入是怎么发生的(从内部1.149 .. 375值到1.15)?

它在文档中指定的位置? toString javadoc并没有真正的帮助(至少我)。

我在语言规范


float和double类型的元素是可以使用IEEE 754 32位单精度和64位
双精度二进制浮点格式表示
的值。


维基百科的IEEE 754文章给出了这个:
$ b


这里给出了从6到9的十进制数字precision(if一个
十进制字符串最多6位有效小数转换为IEEE
754单精度,然后转换回相同数量
有效小数,那么最后一个字符串应该与原始值匹配; / p>

问题2

所以这似乎是Java / IEEE 754浮动应该如何工作?



我可以保证浮点/字符串转换/表示的准确性达到一定数量的数字(比如p和q),数字超过Java会做一些舍入显示(如r)?

感谢您的帮助。

从JLS开始,4.2.4。浮点运算:


Java编程语言要求浮点运算
的行为就像每个浮点运算符四舍五入它的浮点数
导致结果精度。不精确的结果必须舍入到最接近无限精确结果的
可表示值;如果
两个最接近的可表示值相等,则选择其
最小有效位为零的那个。这是IEEE 754标准的
默认舍入模式,称为round to nearest。


Java编程语言在转换
浮动值转换为一个整数(§5.1.3),在这种情况下,它的作用是
,尽管数字被截断,舍弃尾数位。
舍入为零时,格式的值最接近
,并且幅值不会比无限精确的结果大。


Here's what I'm looking at:

float p=1.15f;
BigDecimal bdp=new BigDecimal(p);
float q=1.1499999f;
float r=1.14999999f;

System.out.println(p);   //1.15
System.out.println(bdp); //1.14999997615814208984375
System.out.println(q);   //1.1499999
System.out.println(r);   //1.15

So I understand that the decimal value of "p" 1.15 can't be represented exactly in binary.
And so the large big decimal "bdp" output makes perfect sense to me ... that's the actual value of the float.

Question 1
When the float "p" gets converted back to a string for output (as 1.15), how/where does that rounding occur (from the internal 1.149..375 value to 1.15)?

And where is it specified in the documentation? The toString javadoc doesn't really help (me at least).

I do see this in the language spec:

The elements of the types float and double are those values that can be represented using the IEEE 754 32-bit single-precision and 64-bit double-precision binary floating-point formats, respectively.

Wikipedia's IEEE 754 article gives this:

This gives from 6 to 9 significant decimal digits precision (if a decimal string with at most 6 significant decimal is converted to IEEE 754 single precision and then converted back to the same number of significant decimal, then the final string should match the original;

Question 2
So it seems that this is just how Java/IEEE 754 floats are supposed to work?

I get guaranteed accuracy of float/string conversion/representation up to a certain number of digits (like for "p" and "q"), and if that number of digits is exceeded Java will do some rounding for display (like for "r")?

Thanks for help.

解决方案

From the JLS, 4.2.4. Floating-Point Operations:

The Java programming language requires that floating-point arithmetic behave as if every floating-point operator rounded its floating-point result to the result precision. Inexact results must be rounded to the representable value nearest to the infinitely precise result; if the two nearest representable values are equally near, the one with its least significant bit zero is chosen. This is the IEEE 754 standard's default rounding mode known as round to nearest.

The Java programming language uses round toward zero when converting a floating value to an integer (§5.1.3), which acts, in this case, as though the number were truncated, discarding the mantissa bits. Rounding toward zero chooses at its result the format's value closest to and no greater in magnitude than the infinitely precise result.

这篇关于Java如何将浮点数转换为字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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