Convert String to Double example - Java Language Basics

Java examples for Language Basics:double

Description

Convert String to Double example

Demo Code

 
public class Main {
 
  public static void main(String[] args) {
    //1. Construct Double using constructor.
    Double dObj1 = new Double("100.564");
    System.out.println(dObj1);/*from w ww.ja va2  s. co  m*/
   
    //2. Use valueOf method of Double class. This method is static.
    String str1 = "100.476";
    Double dObj2 = Double.valueOf(str1);
    System.out.println(dObj2);
   
    /*
    To convert a String object to a double primitive value parseDouble method
    of Double class. 
    */
    String str2 = "7.39";
    double d = Double.parseDouble(str2);
    System.out.println(d);
   
  }
}
 

Result


Related Tutorials