How do I convert a string such as 40,123.012345678901
to a double
?
double d = Double.parseDouble("40,123.012345678901");
throws a number format exception.
Thanks
How do I convert a string such as 40,123.012345678901
to a double
?
double d = Double.parseDouble("40,123.012345678901");
throws a number format exception.
Thanks
If there is no way to get rid of comma(,) you may use another approach:
NumberFormat format = NumberFormat.getInstance(Locale.US);
Number number = format.parse("40,123.012345678901");
double d = number.doubleValue();
The simplest way is to remove comma:
double d = Double.parseDouble("40,123.012345678901".replace(",", ""));
A double
is limited to around 16 digits of precision. If you need more precision, you should use BigDecimal.
BigDecimal bd = new BigDecimal("40,123.012345678901".replace(",", ""));
You should remove the comma (,) from your string:
This will work:
double d = Double.parseDouble("40123.012345678901");
Use NumberFormat
to get double
value,
try {
NumberFormat format = NumberFormat.getInstance();
Number parse = format.parse("40,123.012345678901");
System.out.println(parse.doubleValue());
} catch (ParseException ex) {
System.out.println(ex.getMessage());
}