Thursday, 22 September 2016

Converting string to double - Java Program

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 for this conversion.

In Java, Double class provides two methods for converting string to float -

  • 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.

Double.parseDouble example

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.

Double.valueOf() example

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

If conversion 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 - Java Program. If you have any doubt or any suggestions to make please drop a comment. Thanks!


Related Topics

  1. Converting float to string - Java Program
  2. Converting int to string - Java Program
  3. Searching within a String using indexOf(), lastIndexOf() and contains() methods
  4. Converting string to bytearray - Java Program
  5. How to add double quotes to a String

You may also like -

>>>Go to Java Programs page

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