Tuesday, April 11, 2017

How To Convert String to Double


1. Convert Using Double.parseDouble()

You can use Double.parseDouble() to convert a String to a double:
String text = "12.34"; // example String
double value = Double.parseDouble(text);
2. Convert Using Double.parseValueOf()

String numberAsString = "153.25";
double number = Double.valueOf(numberAsString);
System.out.println("The number is: " + number);

3. Convert Using new Double().doubleValue()


String numberAsString = "153.25";
Double doubleObject = new Double(numberAsString);
double number = doubleObject.doubleValue();

4. Convert Using java.text.DecimalFormat

String numberAsString = "153.25";
DecimalFormat decimalFormat = new DecimalFormat("#");
try {
   double number = decimalFormat.parse(numberAsString).doubleValue();
   System.out.println("The number is: " + number);
} catch (ParseException e) {
   System.out.println(numberAsString + " is not a valid number.");
}

0 komentar:

Post a Comment