0xp0打印0.0(十六进制浮点数字)

时间:2012-07-25 14:06:26

标签: java

我只是想知道为什么这会编译?它是什么意思,因为它编译?

System.out.println(0xp0); // p?

输出:

0.0

3 个答案:

答案 0 :(得分:11)

The JLS解释道:

HexadecimalFloatingPointLiteral:
    HexSignificand BinaryExponent FloatTypeSuffixopt

HexSignificand:
    HexNumeral
    HexNumeral .
    0 x HexDigitsopt . HexDigits
    0 X HexDigitsopt . HexDigits

BinaryExponent:
    BinaryExponentIndicator SignedInteger

BinaryExponentIndicator:one of
    p P

基于以上所述,我希望在.HexDigit之前强制p

答案 1 :(得分:9)

这是一个浮点十六进制文字。

  

对于十六进制浮点文字,至少需要一个数字(在整数或小数部分中),并且指数是必需的,浮点类型后缀是可选的。指数由ASCII字母p或P表示,后跟可选的有符号整数。

请参阅规范here

答案 2 :(得分:0)

仅供参考,以下是如何手动将以下内容转换为十进制数字:

double hfpl = 0x1e.18p4;
System.out.println(hfpl); // 481.5

enter image description here