📌  相关文章
📜  如何在Java中通过示例将字符串值转换为浮点值

📅  最后修改于: 2022-05-13 01:55:49.909000             🧑  作者: Mango

如何在Java中通过示例将字符串值转换为浮点值

在Java中给定一个字符串“ str ”,任务是将这个字符串转换为浮点类型。

例子:

Input: str = "1.0"
Output: 1.0

Input: str = "3.14"
Output: 3.14

方法1:(朴素方法)
一种方法是遍历字符串,将数字一一添加到浮点类型中。这种方法不是一种有效的方法。

方法2:(使用 Float.parseFloat() 方法)
最简单的方法是使用Java.lang 包中 Float 类的 parseFloat() 方法。此方法接受要解析的字符串并从中返回浮点类型。如果不可转换,此方法将引发错误。

句法:

Float.parseFloat(str);

下面是上述方法的实现:

示例 1:显示转换成功

// Java Program to convert string to float
  
class GFG {
  
    // Function to convert String to Float
    public static float convertStringToFloat(String str)
    {
  
        // Convert string to float
        // using parseFloat() method
        return Float.parseFloat(str);
    }
  
    // Driver code
    public static void main(String[] args)
    {
  
        // The string value
        String stringValue = "1.0";
  
        // The expected float value
        float floatValue;
  
        // Convert string to float
        floatValue = convertStringToFloat(stringValue);
  
        // Print the expected float value
        System.out.println(
            stringValue
            + " after converting into float = "
            + floatValue);
    }
}
输出:
1.0 after converting into float = 1.0

示例 2:显示不成功的转换

// Java Program to convert string to float
  
class GFG {
  
    // Function to convert String to Float
    public static void convertStringToFloat(String str)
    {
  
        float floatValue;
  
        try {
            // Convert string to float
            // using parseFloat() method
            floatValue = Float.parseFloat(str);
  
            // Print the expected float value
            System.out.println(
                str
                + " after converting into float = "
                + floatValue);
        }
        catch (Exception e) {
            // Print the error
            System.out.println(
                str
                + " cannot be converted to float: "
                + e.getMessage());
        }
    }
  
    // Driver code
    public static void main(String[] args)
    {
  
        // The string value
        String str1 = "";
        String str2 = null;
        String str3 = "GFG";
  
        // Convert string to float
        // using parseFloat() method
        convertStringToFloat(str1);
        convertStringToFloat(str2);
        convertStringToFloat(str3);
    }
}
输出:
cannot be converted to float: empty String
null cannot be converted to float: null
GFG cannot be converted to float: For input string: "GFG"

方法 3:(使用 Float.valueOf() 方法)
Float 类的valueOf() 方法将数据从其内部形式转换为人类可读的形式。

句法:

Float.valueOf(str);

下面是上述方法的实现:

示例 1:显示转换成功

// Java Program to convert string to float
  
class GFG {
  
    // Function to convert String to Float
    public static float convertStringToFloat(String str)
    {
  
        // Convert string to float
        // using valueOf() method
        return Float.valueOf(str);
    }
  
    // Driver code
    public static void main(String[] args)
    {
  
        // The string value
        String stringValue = "1.0";
  
        // The expected float value
        float floatValue;
  
        // Convert string to float
        floatValue = convertStringToFloat(stringValue);
  
        // Print the expected float value
        System.out.println(
            stringValue
            + " after converting into float = "
            + floatValue);
    }
}
输出:
1.0 after converting into float = 1.0