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


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


Java Float 類的 valueOf() 方法返回一個表示指定浮點值 'f' 或字符串參數 's' 的浮點實例。

用法:

1.public static Float valueOf(float f)
2.public  static Float valueOf(String s)

參數:

  • 傳遞浮點型 (f) 參數。
  • 傳遞字符串類型參數。

返回值:

valueOf() 方法返回傳遞的指定浮點數或字符串值的浮點數對象。

例子1

import java.lang.*;

public class FloatValueOfExample1 {
    public static void main(String[] args) {
        //returns a Float instance
        Float f1 = new Float(48654683);
        System.out.println("1. Value = "+Float.valueOf(f1));
        //passing negative integer
        Float f2 = new Float(-6789.87);
        System.out.println("2. Value = "+Float.valueOf(f2));
        Float f3 = new Float (0.0988767);
        System.out.println("3. Value = "+Float.valueOf(f3));
    }
}

輸出:

1. Value = 4.8654684E7
2. Value = -6789.87
3. Value = 0.0988767

例子2

import java.util.Scanner;

public class FloatValueOfExample2 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter your pin code:");
        String str = scanner.nextLine();
        // valueOF() method for String values
        Float f1 = Float.valueOf(str);
        System.out.println(f1);
        Float f2= 121002.0f;
        Float f3 = 110002.f;
        Float f4 = 201301.0f;
        if (f1.equals(f2)) {
            System.out.println("Area = Faridabad.");
        } else if (f1.equals(f3)) {
            System.out.println("Area = Delhi");
        } else if (f1.equals(f4)) {
            System.out.println("Area = Noida");
        }
        else {
           System.out.println("Sorry! area is out of my knowledge.");
        }
    }
}

輸出:

Enter your pin code:121002
121002.0
Area = Faridabad.

例子3

public class FloatValueOfExample3 {
    public static void main(String[] args) {
        Float f1 = Float.POSITIVE_INFINITY;
        System.out.println("1. Value = "+Float.valueOf(f1));

        Float f2 = Float.NEGATIVE_INFINITY;
        System.out.println("2. Value = "+Float.valueOf(f2));

        Float f3 = Float.MIN_NORMAL;
        System.out.println("3. Value = "+Float.valueOf(f3));

        Float f4 = Float.MAX_VALUE;
        System.out.println("4. Value = "+Float.valueOf(f4));

        Float f5 =Float.MIN_VALUE;
        System.out.println("5. Value = "+Float.valueOf(f5));

        Float f6 =Float.NaN;
        System.out.println("6. Value = "+Float.valueOf(f6));


    }
}

輸出:

1. Value = Infinity
2. Value = -Infinity
3. Value = 1.17549435E-38
4. Value = 3.4028235E38
5. Value = 1.4E-45
6. Value = NaN

示例 4

import java.util.Scanner;

public class FloatValueOfExample4 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter string value:");
        String str = scanner.nextLine();
        Float f1 = Float.valueOf(str);
        System.out.println("Float value = "+f1);
    }
}

輸出:

Enter string value:reema
Exception in thread "main" java.lang.NumberFormatException:For input string:"reema"
	at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:2043)
	at sun.misc.FloatingDecimal.parseFloat(FloatingDecimal.java:122)
	at java.lang.Float.parseFloat(Float.java:451)
	at java.lang.Float.valueOf(Float.java:416)
	at Float_valueOfMethodExample4.main(Float_valueOfMethodExample4.java:8)

它會給我們運行時錯誤,因為字符串值 reema 不能轉換為 Float,所以應該檢查這個運行時異常。







相關用法


注:本文由純淨天空篩選整理自 Java Float valueOf() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。