My server is returning a string like 2,100.00.
I get a NumberFormatException when I try to convert this String into a double.
Double.valueOf("2,100.00");
Do I have to remove the decimal/comma, convert, and add the decimal back in?
You could set a custom separator in a decimal format
DecimalFormat df = new DecimalFormat();
DecimalFormatSymbols symbols = new DecimalFormatSymbols();
symbols.setDecimalSeparator('.');
symbols.setGroupingSeparator(',');
df.setDecimalFormatSymbols(symbols);
Number number = df.parse(str);
Then you can use the number
. To convert it to double you can do number.doubleValue()
Yes you have to remove the comma. This solution would be more simple:
String str = "2,100.00";
Double.valueOf(str.replaceAll(",", ""));
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.