Java - Float toHexString() 方法



描述

Java Float toHexString() 方法返回浮點數引數 f 的十六進位制字串表示形式。這裡可以看到一些示例:

浮點值 十六進位制字串
1.0 0x1.0p0
-1.0 -0x1.0p0
2.0 0x1.0p1
3.0 0x1.8p1
0.5 0x1.0p-1
0.25 0x1.0p-2
Float.MAX_VALUE 0x1.fffffep127
最小規格化值 0x1.0p-126
最大非規格化值 0x0.fffffep-126
Float.MIN_VALUE 0x0.000002p-126

宣告

以下是 java.lang.Float.toHexString() 方法的宣告

public static String toHexString(float f)

引數

f − 要轉換的浮點數。

返回值

此方法返回引數的十六進位制字串表示形式。

異常

獲取正浮點值的十六進位制字串表示形式示例

以下示例演示瞭如何使用 Float toHexString(float) 方法獲取浮點數的十六進位制字串表示形式。我們展示了正浮點值的多個十六進位制表示形式以及最大浮點值的十六進位制表示形式。

package com.tutorialspoint;

public class FloatDemo {
   public static void main(String[] args) {

      /* returns a hexadecimal string representation of the
         float argument */
      String str = Float.toHexString(1.0f);
      System.out.println("Hex String = " + str);
    
      str = Float.toHexString(3.0f);
      System.out.println("Hex String = " + str);
    
      str = Float.toHexString(0.25f);
      System.out.println("Hex String = " + str);
    
      str = Float.toHexString(Float.MAX_VALUE);
      System.out.println("Hex String = " + str);
   }
}  

輸出

讓我們編譯並執行以上程式,這將產生以下結果:

Hex String = 0x1.0p0
Hex String = 0x1.8p1
Hex String = 0x1.0p-2
Hex String = 0x1.fffffep127

獲取負浮點值的十六進位制字串表示形式示例

以下示例演示瞭如何使用 Float toHexString(float) 方法獲取浮點數的十六進位制字串表示形式。我們展示了負浮點值的多個十六進位制表示形式以及最小浮點值的十六進位制表示形式。

package com.tutorialspoint;
public class FloatDemo {
   public static void main(String[] args) {

      /* returns a hexadecimal string representation of the
         float argument */
      String str = Float.toHexString(-1.0f);
      System.out.println("Hex String = " + str);
    
      str = Float.toHexString(-3.0f);
      System.out.println("Hex String = " + str);
    
      str = Float.toHexString(-0.25f);
      System.out.println("Hex String = " + str);
    
      str = Float.toHexString(Float.MIN_VALUE);
      System.out.println("Hex String = " + str);
   }
}  

輸出

讓我們編譯並執行以上程式,這將產生以下結果:

Hex String = -0x1.0p0
Hex String = -0x1.8p1
Hex String = -0x1.0p-2
Hex String = 0x0.000002p-126

獲取零浮點值的十六進位制字串表示形式示例

以下示例演示瞭如何使用 Float toHexString(float) 方法獲取浮點數的十六進位制字串表示形式。我們展示了負零和正零值的多個十六進位制表示形式。

package com.tutorialspoint;
public class FloatDemo {
   public static void main(String[] args) {

      /* returns a hexadecimal string representation of the
         float argument */
      String str = Float.toHexString(-0.0f);
      System.out.println("Hex String = " + str);
    
      str = Float.toHexString(0.0f);
      System.out.println("Hex String = " + str);
   }
}  

輸出

讓我們編譯並執行以上程式,這將產生以下結果:

Hex String = -0x0.0p0
Hex String = 0x0.0p0
java_lang_float.htm
廣告