當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Java Float toHexString()用法及代碼示例

Float類toHexString()方法

  • toHexString() 方法可在java.lang包。
  • toHexString() 方法用於表示給定參數 [value] 的單精度浮點類型的十六進製字符串。
  • toHexString() 方法是一個靜態方法,它也可以通過類名訪問,如果我們嘗試使用類對象訪問該方法,那麽我們也不會收到錯誤。
  • toHexString() 方法在從浮點數轉換為十六進製字符串時不會拋出異常。

用法:

    public static String toHexString (float value);

參數:

  • float value– 表示要轉換的浮點值。

返回值:

這個方法的返回類型是String,它返回表示浮點值的給定參數的十六進製字符串。

例:

// Java program to demonstrate the example 
// of toHexString(float value) method of Float class

public class ToHexStringOfFloatClass {
    public static void main(String[] args) {
        // Variables initialization
        float f1 = 10.0f;
        float f2 = 20.0f;
        float f3 = 30.0f;
        float f4 = Float.MAX_VALUE;
        float f5 = Float.MIN_VALUE;

        // Float instance creation
        Float value = new Float(f1);

        // It represents hexadecimal string of the given
        // float type f2 argument
        String s = value.toHexString(f2);

        // Display Hexadecimal String Representation
        System.out.println("value.toHexString(f2):" + s);

        // It represents hexadecimal string of the given
        // float type f3 argument
        s = value.toHexString(f3);

        // Display Hexadecimal String Representation
        System.out.println("value.toHexString(f3):" + s);

        // It represents hexadecimal string of the given
        // float type f4 argument
        s = value.toHexString(f4);

        // Display Hexadecimal String Representation
        System.out.println("value.toHexString(f4):" + s);

        // It represents hexadecimal string of the given
        // float type f5 argument
        s = value.toHexString(f5);

        // Display Hexadecimal String Representation
        System.out.println("value.toHexString(f5):" + s);
    }
}

輸出

value.toHexString(f2):0x1.4p4
value.toHexString(f3):0x1.ep4
value.toHexString(f4):0x1.fffffep127
value.toHexString(f5):0x0.000002p-126


相關用法


注:本文由純淨天空篩選整理自Preeti Jain大神的英文原創作品 Java Float class toHexString() method with example。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。