How to Convert String to double in Java

To convert String to double in Java you can use one of the following options-

  1. Double.parseDouble(String s)– Returns a new double initialized to the value represented by the specified String. Throws NumberFormatException if the string does not contain a parsable double.
  2. Double.valueOf(String s)– Returns a Double object holding the double value represented by the argument string s. Throws NumberFormatException if the string does not contain a parsable number.

As you can see parseDouble() method returns a double primitive where as valueOf() method returns a Double object.

Java example to convert String to double using Double.parseDouble

public class StringToDouble {
  public static void main(String[] args) {
    String num = "145.34526";
    try{
      double d = Double.parseDouble(num);
      System.out.println("Value- " + d);
      // can be used in arithmetic operations now
      System.out.println(d+"/3 = " + d/3);
    }catch(NumberFormatException ex){
      System.out.println("Error while conversion " + ex.getMessage());
    }
  }
}

Output

Value- 145.34526
145.34526/3 = 48.44842

For double numbers you can use “d” or “D” (even f or F which denotes double) so a String like this – “145.34d” won’t result in NumberFormatException while converting. But having any other alphabet like “145.34c” will throw exception.

public class StringToDouble {

	public static void main(String[] args) {
		String num = "145.34526d";
		try{
			double d = Double.parseDouble(num);
			System.out.println("Value- " + d);
			// can be used in arithmetic operations now
			System.out.println(d+"/3 = " + d/3);
		}catch(NumberFormatException ex){
			System.out.println("Error while conversion " + ex.getMessage());
		}
	}
}

Output

Value- 145.34526
145.34526/3 = 48.44842

Java example to convert String to double using Double.valueOf

public class StringToDouble {

	public static void main(String[] args) {
		String str = "-245.67456";
		try{
			Double d = Double.valueOf(str);
			System.out.println("value- " + d);
		}catch(NumberFormatException exp){
			System.out.println("Error in conversion " + exp.getMessage());
			throw exp;
		} 
	}
}

Output

value- -245.67456

NumberFormatException

While converting string to double in Java NumberFormatException is thrown if an invalid number string is passed for conversion.

public class StringToDouble {

	public static void main(String[] args) {
		String str = "45.674c";
		try{
			Double d = Double.valueOf(str);
			System.out.println("value- " + d);
		}catch(NumberFormatException exp){
			System.out.println("Error in conversion " + exp.getMessage());
			throw exp;
		} 
	}
}

Output

Error in conversion For input string: "45.674c"Exception in thread "main" 
java.lang.NumberFormatException: For input string: "45.674c"
	at java.base/jdk.internal.math.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:2054)
	at java.base/jdk.internal.math.FloatingDecimal.parseDouble(FloatingDecimal.java:110)
	at java.base/java.lang.Double.parseDouble(Double.java:543)
	at java.base/java.lang.Double.valueOf(Double.java:506)
	at com.knpcode.programs.StringToDouble.main(StringToDouble.java:8)

Related Posts

That’s all for the topic How to Convert String to double in Java. If something is missing or you have something to share about the topic please write a comment.


You may also like

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.