当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。