📜  在Java中将String转换为Double

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

在Java中将String转换为Double

在这里,我们将在Java中将 String 转换为 Double 。此转换有 3 种方法,如下所述:

插图:

Input : String = "20.156"
Output: 20.156
Input  : String = "456.21"
Output : 456.21

将字符串转换为双精度的不同方法

  1. 使用 Double 类的 parseDouble() 方法
  2. 使用 Double 类的 valueOf() 方法
  3. 使用 Double 类的构造函数

方法一:使用 Double 类的 parseDouble() 方法

句法:

double str1 = Double.parseDouble(str); 

例子:

Java
// Java program to convert String to Double
// Using parseDouble() Method of Double Class
 
// Main class
public class GFG {
 
    // Main driver method
    public static void main(String args[])
    {
 
        // Create and initializing a string
        String str = "2033.12244";
 
        // Converting the above string into Double
        // using parseDouble() Method
        double str1 = Double.parseDouble(str);
 
        // Printing string as Double type
        System.out.println(str1);
    }
}


Java
// Java program to convert String to Double
// using valueOf() Method of Double Class
 
// Main class
public class GFG {
 
    // Main driver method
    public static void main(String args[])
    {
 
        // Creating and initializing a string
        String str = "2033.12244";
 
        // Converting the above string to Double type
        double str1 = Double.valueOf(str);
 
        // Printing above string as double type
        System.out.println(str1);
    }
}


Java
// Java program to convert String to Double
// Using Constructor of Double class
 
// Main class
public class GFG {
 
    // Main driver method
    public static void main(String args[])
    {
 
        // Creating and initializing a string
        String str = "2033.12244";
 
        // Converting above string into double type
        Double str1 = new Double(str);
 
        // print above string as Double type
        System.out.println(str1);
    }
}


输出:
2033.12244

方法二:使用 Double 类的 valueOf() 方法

句法:

double str1 = Double.valueOf(str); 

例子:

Java

// Java program to convert String to Double
// using valueOf() Method of Double Class
 
// Main class
public class GFG {
 
    // Main driver method
    public static void main(String args[])
    {
 
        // Creating and initializing a string
        String str = "2033.12244";
 
        // Converting the above string to Double type
        double str1 = Double.valueOf(str);
 
        // Printing above string as double type
        System.out.println(str1);
    }
}
输出:
2033.12244

方法3:使用Double类的构造函数

句法:

Double str1 = new Double(str); 

例子:

Java

// Java program to convert String to Double
// Using Constructor of Double class
 
// Main class
public class GFG {
 
    // Main driver method
    public static void main(String args[])
    {
 
        // Creating and initializing a string
        String str = "2033.12244";
 
        // Converting above string into double type
        Double str1 = new Double(str);
 
        // print above string as Double type
        System.out.println(str1);
    }
}
输出:
2033.12244