英文:
BigDecimal NumberFormatException when I execute the application in another computer
问题
大家好,我在执行这段代码时遇到了错误。在我的电脑上运行得很好,但是当我将应用程序发送给朋友尝试时,却出现了数字格式异常。代码有问题吗?如果有问题,为什么在我的电脑上却能正常工作?附注:我在另一台计算机上尝试过,它能正常运行。
double total = 0;
// 这里有一个for循环来改变total的值
// 但在这种情况下(应用程序启动时),循环条件将为false
// 所以total将保持为0
String totalS = String.format("%.2f", total);
if (totalS.endsWith(".00")) {
totalS = totalS.substring(0, totalS.length() - 3);
}
lblDa.setText(new BigDecimal(totalS).toPlainString() + " DA");
编辑:这是错误消息
java.lang.NumberFormatException: 字符“,”既不是十进制数字,也不是小数点,也不是“e”表示指数的标记。
at java.base/java.math.BigDecimal.<init>(BigDecimal.java:519)
at java.base/java.math.BigDecimal.<init>(BigDecimal.java:402)
at java.base/java.math.BigDecimal.<init>(BigDecimal.java:835)
at invpack.MainFrame.updateTotal(MainFrame.java:3052)
at invpack.MainFrame.initialize(MainFrame.java:557)
at invpack.MainFrame.<init>(MainFrame.java:185)
at invpack.MainFrame$1.run(MainFrame.java:171)
at java.desktop/java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:316)
at java.desktop/java.awt.EventQueue.dispatchEventImpl(EventQueue.java:770)
at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:721)
at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:715)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:85)
at java.desktop/java.awt.EventQueue.dispatchEvent(EventQueue.java:740)
at java.desktop/java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:203)
at java.desktop/java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:124)
at java.desktop/java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:113)
at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:109)
at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.desktop/java.awt.EventDispatchThread.run(EventDispatchThread.java:90)
英文:
Hello guys I'm having an error when executing this code
it works perfectly in my computer but when I sent the application to my friend to try it it caused a number format exception, is there a problem with the code? if so why is it working in my computer
ps: I tried the application in another computer and it works
double total = 0;
//here there is a for loop changing the total value
//but in this case (when the application starts) the loop condition will be false
//so the total will stay 0
String totalS = String.format("%.2f", total);
if (totalS.endsWith(".00")) {
totalS = totalS.substring(0,totalS.length()-3);
}
lblDa.setText(new BigDecimal(totalS).toPlainString() +" DA");
edit: here is the error message
java.lang.NumberFormatException: Character , is neither a decimal digit number, decimal point, nor "e" notation exponential mark.
at java.base/java.math.BigDecimal.<init>(BigDecimal.java:519)
at java.base/java.math.BigDecimal.<init>(BigDecimal.java:402)
at java.base/java.math.BigDecimal.<init>(BigDecimal.java:835)
at invpack.MainFrame.updateTotal(MainFrame.java:3052)
at invpack.MainFrame.initialize(MainFrame.java:557)
at invpack.MainFrame.<init>(MainFrame.java:185)
at invpack.MainFrame$1.run(MainFrame.java:171)
at java.desktop/java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:316)
at java.desktop/java.awt.EventQueue.dispatchEventImpl(EventQueue.java:770)
at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:721)
at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:715)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:85)
at java.desktop/java.awt.EventQueue.dispatchEvent(EventQueue.java:740)
at java.desktop/java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:203)
at java.desktop/java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:124)
at java.desktop/java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:113)
at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:109)
at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.desktop/java.awt.EventDispatchThread.run(EventDispatchThread.java:90)
答案1
得分: 1
The String.format
method you are using looks up details about the number format from user preferences: it may use "," instead of "." for the decimal separator, it may even use digits other than 0-9. The rest of your program assumes that the generated string uses ASCII digits and "." for decimal separator.
When you need String.format
to generate the number in a specific style, pass in a fixed Locale argument:
String totalS = String.format(Locale.ROOT, "%.2f", total);
On the other hand, it looks like all of this code is there only so that you don't display trailing zeros. There's a more straightforward way to achieve that: use a number format that makes trailing zeros optional.
DecimalFormat formatter = new DecimalFormat("0.##");
lblDa.setText(formatter.format(total) + " DA");
英文:
The String.format
method you are using looks up details about the number format from user preferences: it may use "," instead of "." for the decimal separator, it may even use digits other than 0-9. The rest of your program assumes that the generated string uses ASCII digits and "." for decimal separator.
When you need String.format
to generate the number in a specific style, pass in a fixed Locale argument:
String totalS = String.format(Locale.ROOT, "%.2f", total);
On the other hand, it looks like all of this code is there only so that you don't display trailing zeros. There's a more straightforward way to achieve that: use a number format that makes trailing zeros optional.
DecimalFormat formatter = new DecimalFormat("0.##");
lblDa.setText(formatter.format(total) +" DA");
答案2
得分: 0
s = new DecimalFormat("0.####").format(Double.parseDouble(value))
它将从任意双精度浮点数值中移除前导零!
英文:
String s = new DecimalFormat("0.####").format(Double.parseDouble(value));
It will remove any leading zeros from any double value!
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论