java.lang.NumberFormatException: For input string: “20,475.00”

♀尐吖头ヾ 提交于 2019-12-01 06:41:24

Since Float.parseFloat() and Float.valueOf() always will assume that the number is in your local format, here's a short example how to do localized parsing if your locale does not match the number format you're getting.

String str = "20,475.00";
NumberFormat nf = NumberFormat.getInstance(Locale.US); // Looks like a US format
float f = nf.parse(str).floatValue();

Use NumberFormat class instead of Float.parseFloat. It will allow you to specify a format for parsing a formatted number such as 20,475.00.

Example:

NumberFormat nf = NumberFormat.getInstance();
// provided your Locale information matches your number format
sum += nf.parse(tableLedger.getModel().getValueAt(i, 2).toString());

Your number is locale-dependent. Normaly a number can only contain optional minus sign at the beginning and optional decimal dot. Comma-separated thousands are not supported.

NumberFormat class, as Pablo Santa Cruz suggested, allows you to specify your own format of numbers and parse according to that format.

If you are going to use decimals, I can suggest using DecimalFormat

I tried this two line to convert string into double

NumberFormat nf = NumberFormat.getInstance()
dounum=nf.parse("24,000.50").toDouble()
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!