Convert string value to double

ReturnMethodSummary
static doubleparseDouble(String s)Returns a double to the value represented by the String.
static DoublevalueOf(double d)Returns a Double instance representing the double value.
static DoublevalueOf(String s)Returns a Double object holding the double value represented by the argument string s.

public class Main {
  public static void main(String[] args) {
    
    System.out.println(Double.valueOf("1.234"));

  }
}

The output:


1.234

Double.valueOf can also be used to check if a string is a number.


public class Main {
  public static void main(String[] args) {
    
    System.out.println(Double.valueOf("1.abc"));

  }
}

The code above produces the following error message:


Exception in thread "main" java.lang.NumberFormatException: For input string: "1.abc"
    at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1224)
    at java.lang.Double.valueOf(Double.java:475)
    at Main.main(Main.java:4)

You can use the Double.parseDouble in the same way
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.