java - Double.toHexString() 语法 - 它来自哪里?

标签 java double double-precision

    System.out.println(Double.toHexString(-1d));

产量

-0x1.0p0

Javadoc语法解释如下:

If m is a double value with a normalized representation, substrings are used to represent the significand and exponent fields. The significand is represented by the characters "0x1." followed by a lowercase hexadecimal representation of the rest of the significand as a fraction. Trailing zeros in the hexadecimal representation are removed unless all the digits are zero, in which case a single zero is used. Next, the exponent is represented by "p" followed by a decimal string of the unbiased exponent as if produced by a call to Integer.toString on the exponent value.

这是我第一次看到这些“-0x1”。注释。是某种行业标准,还是 Java 的创新?

最佳答案

不仅仅是java,看起来C也是这样,这个页面解释了一些:http://www.exploringbinary.com/hexadecimal-floating-point-constants/

由于十六进制数表示 double ,因此它以科学记数法表示,因此您看到的是 1.0 的 0 次方。问题是 toHexString 方法首先查看二进制字符串,然后将任何值转换为“.”之后的部分为十六进制。这意味着句点之前的数字始终是 1,因此 javadoc 谈论“0x1”作为将数字描述为十六进制的表示法的原因。 “-”只是表示它是否定的,它不是符号的一部分。

关于java - Double.toHexString() 语法 - 它来自哪里?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24805866/

相关文章:

java - 生成随机牌照

java - double 等于一个整数总是会转换为该整数吗?

C# IEEE754 舍入

Java float 比 double 更精确?

c# - 如何知道双字符串是否往返安全?

C# 高 double

java - 在 html 代码标签中设置文本样式

java - 从 Android 中单独的 AsyncTask 类连接 WiFi 管理器

Java : How to count the number of decimals contained in a double value that represent money?

java - Java 中的 Kotlin 结果类型