Wednesday, February 21, 2018

Converting String to double in Java

There are several instances when you want to convert a string to double data type so in this post we’ll see what different ways do we have to convert String to double in Java.

Double class in Java provides two methods for converting String to 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.
  • 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. If s is null, then a NullPointerException is thrown.

Here Few things to note are-

  1. Both of the methods are static so you can use them directly with the class i.e. Double.parseDouble(String s) and Double.valueOf(String s).
  2. If you have noticed parseDouble method returns double (primitive data type) where as valueOf() method returns Double class object.
  3. String passed should have digits only except that the first character may be an ASCII minus sign '-' ('\u002D') to indicate a negative value or an ASCII plus sign '+' ('\u002B') to indicate a positive value.
  4. You can use “d” or “D” (even f or F which denotes float) with the value so having string as “43.67d” will not throw NumberFormatException while converting where as “43.67e” will throw exception.

Note that Double class also has a constructor that takes String as argument so that is also one way to convert string to float.

Double(String s)- Constructs a newly allocated Double object that represents the floating-point value of type double represented by the string.

Converting String to double in Java using Double.parseDouble method

public class StringToDouble {

 public static void main(String[] args) {
  String num = "45.78678968d";
  try{
   double value = Double.parseDouble(num);
   System.out.println("value " + value);
  }catch(NumberFormatException neExp){
   System.out.println("Error while conversion " + neExp.getMessage());
  }
 }
}

Output

value 45.78678968

Since double has double precision and occupies 8 bytes so you will get better precision than float.

Converting String to double in Java using Double.valueOf() method

public class StringToDouble {
 public static void main(String[] args) {
  String num = "-45.0784";
  try{
   Double value = Double.valueOf(num);
   System.out.println("value " + value);
  }catch(NumberFormatException neExp){
   System.out.println("Error while conversion " + neExp.getMessage());
  }
 }
}
Output
value -45.0784

Note here that if the sign is negative, the first character of the result is '-' , if the sign is positive, no sign character appears in the result.

NumberFormatException while converting String to double

If conversion from String to double fails then NumberFormatExcpetion is thrown. So, it is better to enclose your conversion code with in a try-catch block.

public class StringToDouble {

 public static void main(String[] args) {
  String num = "45.0784e";
  try{
   Double value = Double.valueOf(num);
   System.out.println("value " + value);
  }catch(NumberFormatException neExp){
   System.out.println("Error while conversion " + neExp.getMessage());
  }
 }
}

Output

Error while conversion For input string: "45.0784e"

Note here that String has alphabet e also along with the digits. Which results in NumberFormatException.

That's all for this topic Converting String to double in Java. If you have any doubt or any suggestions to make please drop a comment. Thanks!

>>>Return to Java Programs Page


Related Topics

  1. Converting float to string - Java Program
  2. Converting int to string - Java Program
  3. Converting String to Bytearray - Java Program
  4. How to Add Double Quotes to a String - Java Program
  5. Searching within a String using indexOf(), lastIndexOf() and contains() methods

You may also like-

  1. How to Create Deadlock in Java Multi-Threading - Java Program
  2. How to Read File From The Last Line in Java
  3. Java Collections Interview Questions
  4. Method overriding in Java
  5. Difference between Encapsulation and Abstraction in Java
  6. Difference between throw and throws
  7. Why wait(), notify() and notifyAll() methods are in Object class
  8. What is Dependency Injection in Spring

1 comment:

  1. Very nice post here thanks for it .I always like and such a super contents of these
    post.Excellent and very cool idea and great content of different kinds of the valuable
    information's. seo company in chennai

    ReplyDelete