Java Convert String To Double Example

In this article, we will show you how to convert Java String to wrapper Double class or primitive type double code examples. There are different ways we can convert Java String to wrapper Double class or primitive type double.

In this article, we will show you how to convert Java String to wrapper Double class or primitive type double examples. There are different ways we can convert Java String to wrapper Double class or primitive type double. This is a common scenario to convert Java String to wrapper Double class or primitive type double in java programming language.
In this post, we will demonstrate converting Java String to wrapper Double class or primitive type double in 4 ways.
  1. Convert using Double.parseDouble()
  2. Convert using Double.valueOf()
  3. Convert using new Double(String).doubleValue()
  4. Convert using DecimalFormat

1. Convert using Double.parseDouble()

Double.parseDouble() - Returns a new double initialized to the value represented by the specified String, as performed by the valueOf method of class Double.
// Convert using Double.parseDouble()
String str = "153.25";
double number = Double.parseDouble(str);
System.out.println("The number is: " + number);
Output:
The number is: 153.25

2. Convert using Double.valueOf()

//Convert using Double.valueOf()
String str = "153.25";
double number1 = Double.valueOf(str);
System.out.println("The number is: " + number1);
Output:
The number is: 153.25

3. Convert using new Double(String).doubleValue()

// Convert using new Double(String).doubleValue()
String str = "153.25";
Double doubleObject = new Double(str);
double number2 = doubleObject.doubleValue();
System.out.println("The number is: " + number2);
Output:
The number is: 153.25

4. Convert using DecimalFormat

//Convert using DecimalFormat
String str = "153.25";
DecimalFormat decimalFormat = new DecimalFormat("#");
try {
   double number3 = decimalFormat.parse(str).doubleValue();
   System.out.println("The number is: " + number3);
} catch (ParseException e) {
   System.out.println(str + " is not a valid number.");
}
Output:
The number is: 153.25

Complete Program for Reference

import java.text.DecimalFormat;
import java.text.ParseException;

/**
 * ConvertStringToDoupleExamples
 * @author javaguides.net
 *
 */
public class ConvertStringToDoupleExamples {
 public static void main(String[] args) {
  // Convert using Double.parseDouble()
  String str = "153.25";
  double number = Double.parseDouble(str);
  System.out.println("The number is: " + number);
  
  //Convert using Double.valueOf()
  double number1 = Double.valueOf(str);
  System.out.println("The number is: " + number1);
  
  // Convert using new Double(String).doubleValue()
  Double doubleObject = new Double(str);
  double number2 = doubleObject.doubleValue();
  System.out.println("The number is: " + number2);
  
  //Convert using DecimalFormat
  DecimalFormat decimalFormat = new DecimalFormat("#");
  try {
     double number3 = decimalFormat.parse(str).doubleValue();
     System.out.println("The number is: " + number3);
  } catch (ParseException e) {
     System.out.println(str + " is not a valid number.");
  }
 }
}
Output:
The number is: 153.25
The number is: 153.25
The number is: 153.25
The number is: 153.25


Comments