当我在另一台计算机上执行应用程序时出现 BigDecimal NumberFormatException

BigDecimal NumberFormatException when I execute the application in another computer

大家好我在执行这段代码时遇到了错误 它在我的电脑上运行完美,但是当我将应用程序发送给我的朋友尝试时它导致了数字格式异常,代码有问题吗?如果是这样,为什么它在我的电脑上工作 ps: 我在另一台电脑上试过这个应用程序,它可以工作

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

编辑:这是错误信息

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.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.run(EventQueue.java:721)
        at java.desktop/java.awt.EventQueue.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)

您正在使用的 String.format 方法从用户首选项中查找有关数字格式的详细信息:它可能使用“,”而不是“.”对于小数点分隔符,它甚至可以使用 0-9 以外的数字。程序的其余部分假定生成的字符串使用 ASCII 数字和“.”。用于小数分隔符。

当您需要String.format生成特定样式的数字时,传入一个固定的Locale参数:

String totalS = String.format(Locale.ROOT, "%.2f", total);

另一方面,看起来所有这些代码都只是为了不显示尾随零。有一种更直接的方法可以实现这一点:使用一种使尾随零成为可选的数字格式。

DecimalFormat formatter = new DecimalFormat("0.##");
lblDa.setText(formatter.format(total) +" DA");

String s = new DecimalFormat("0.####").format(Double.parseDouble(value));

它将删除任何双精度值中的所有前导零!