String to Double Example in Java

public class StringToDouble {
	public static void main(String[] args) {

		String stringValue="120.54";
		
		//Constructs a newly allocated Double object
		//NumberFormatException will be thrown if the string does not contain a parsable number
		Double doubleValue1 = new Double(stringValue);
		System.out.println(doubleValue1);
		
		//Returns a Double object holding the double value represented by the string. 
		//NumberFormatException will be thrown if the string does not contain a parsable number
		Double doubleValue2 = Double.valueOf(stringValue);
		System.out.println(doubleValue2);
		
		//Returns a new double primitive of Stringvalue
		//If stringValue is null, then a NullPointerException 
		//NumberFormatException will be thrown if the string does not contain a parsable number
		double doubleValue3= Double.parseDouble(stringValue);
		System.out.println(doubleValue3);
		
	}
	
}
Output
120.54 120.54 120.54










Related Examples


Write a Comparable to compare students based upon their age?
Simple Array Sorting Program
Write a program to convert from decimal to binary?
Write a program to get list of all the files from a given directory?
Producer Consumer Example in Thread
Write a program to reverse a String?
Write a Comparator to compare students based upon their name and age?








Receive Latest Java Examples in Your Email: