java.lang.NumberFormatException:对于输入字符串:“20,475.00” [英] java.lang.NumberFormatException: For input string: "20,475.00"

查看:346
本文介绍了java.lang.NumberFormatException:对于输入字符串:“20,475.00”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试获得系统的运行平衡。为此,我从列AMOUNT获取jtable中所有数字的总和,并将总和减去txtLoanAmount中的值。
这是我的代码片段:

i am trying to get the running balance of my system. To do it, i get the sum of all numbers in the jtable from column AMOUNT and subtract the sum to the value inside the txtLoanAmount. here's my code snippet:

String LoanAmount = txtLoanAmount.getText();
float f = Float.valueOf(LoanAmount.trim()).floatValue();
float balance = 0; 
float sum = 0;

for(int i=0;i<=tableLedger.getRowCount()-1;i++) {
    sum = sum + Float.parseFloat(tableLedger.getModel().getValueAt(i, 2).toString());
}
balance = f - sum;
System.out.println(balance);

现在我收到错误消息:

Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: "20,475.00"

我该如何解决这个问题?任何帮助将不胜感激。谢谢

How can i solve this ? any help will be appreciated. Thanks

推荐答案

由于Float.parseFloat()和Float.valueOf()总是假设该数字是您的本地格式,这里有一个简短的例子,说明如果您的语言环境与您获得的数字格式不匹配,如何进行本地化解析。

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();

这篇关于java.lang.NumberFormatException:对于输入字符串:“20,475.00”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆