十进制格式化程序无法解析为正确的形式?

发布于 2025-01-13 07:34:08 字数 1336 浏览 2 评论 0原文

我正在制作一个 toto 应用程序,它应该返回所有游戏的统计数据。它应该返回 team#1、team#2平局 的获胜百分比,并带有 2 个摩擦数字。 我尝试用以下方法来做到这一点:

DecimalFormat dec = new DecimalFormat("##,##");
double firstTeamWinPercentage = firstTeamWinCounter/totalMatches*100;
    double secondTeamWinPercentage = secondTeamWinCounter/totalMatches*100;
    double drawPercentage = drawPercentageCounter/totalMatches*100;

    statistics.setFirstTeamWinPercentage(Double.parseDouble(dec.format(firstTeamWinPercentage)));
    statistics.setSecondTeamWinPrecentage(Double.parseDouble(dec.format(secondTeamWinPercentage)));
    statistics.setDrawPercentage(Double.parseDouble(dec.format(drawPercentage)));

但是不知怎的,它并没有像它应该做的那样进行解析。使用这种方法,统计数据如下所示: 统计数据:#1 队获胜:45.0 %,#2 队获胜:29.0 %,平局:26.0 %

如果我添加 dec.setMinimumFractionDigits(2);,则以下内容抛出异常:

Exception in thread "main" java.lang.NumberFormatException: For input string: "45,09"
at java.base/jdk.internal.math.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:2054)
at java.base/jdk.internal.math.FloatingDecimal.parseDouble(FloatingDecimal.java:110)

我使用 jdk-17。有什么想法为什么会这样抛出吗? 我已经尝试使用 DecimalFormat dec = new DecimalFormat("00,00");DecimalFormat dec = new DecimalFormat("##,00"); 他们也会抛出异常。

Im making a toto app, which should return the Statistics of all games. It should return the win percentage of team#1, team#2 and draws with 2 friction digits.
I tried to do it with the following:

DecimalFormat dec = new DecimalFormat("##,##");
double firstTeamWinPercentage = firstTeamWinCounter/totalMatches*100;
    double secondTeamWinPercentage = secondTeamWinCounter/totalMatches*100;
    double drawPercentage = drawPercentageCounter/totalMatches*100;

    statistics.setFirstTeamWinPercentage(Double.parseDouble(dec.format(firstTeamWinPercentage)));
    statistics.setSecondTeamWinPrecentage(Double.parseDouble(dec.format(secondTeamWinPercentage)));
    statistics.setDrawPercentage(Double.parseDouble(dec.format(drawPercentage)));

However somehow it doesn't parse like it should do. With this method, the Statistics looks like this:
Statistics: Team #1 won: 45.0 %, Team #2 won: 29.0 %, draw: 26.0 %

If I add dec.setMinimumFractionDigits(2);, then the following exception is thrown:

Exception in thread "main" java.lang.NumberFormatException: For input string: "45,09"
at java.base/jdk.internal.math.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:2054)
at java.base/jdk.internal.math.FloatingDecimal.parseDouble(FloatingDecimal.java:110)

Im using jdk-17. Any ideas why it is thrown like this?
I already tried using DecimalFormat dec = new DecimalFormat("00,00");
and DecimalFormat dec = new DecimalFormat("##,00");
They also throw exceptions.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

心碎无痕… 2025-01-20 07:34:08

我未能使用 DecimalFormat 来将我的变量解析为两位小数。但是,以下解决方案也可以正常工作:

 BigDecimal twoDecimalFirstTeamWinPercentageBigDecimal = new BigDecimal(twoDecimalFirstTeamWinPercentage).setScale(2, RoundingMode.HALF_UP);

where

twoDecimalFirstTeamWinPercentage

is a double

I failed to make use of the DecimalFormat in order to parse my variables to two digit decimals. However The following sollution works fine aswell:

 BigDecimal twoDecimalFirstTeamWinPercentageBigDecimal = new BigDecimal(twoDecimalFirstTeamWinPercentage).setScale(2, RoundingMode.HALF_UP);

where

twoDecimalFirstTeamWinPercentage

is a double

悟红尘 2025-01-20 07:34:08

将双精度型格式化为字符串,然后再次将其解析为双精度型是没有意义的。根据您生成字符串的方式,尾随零将被删除。

如果需要两位小数,可以使用 String#format

double d = 1.2;
String s = String.format("Your value = %.2f", d);
System.out.println(s); // Your value = 1.20

您的问题缺少关键部分:如何Statistics#toString 实施了吗? (或者任何方法为您的统计类生成字符串输出。

There's no point in formatting your double to a string, then parsing it to a double again. Depending on how you generate your string, trailing zeroes will be stripped.

If you require two decimal places, you could use String#format:

double d = 1.2;
String s = String.format("Your value = %.2f", d);
System.out.println(s); // Your value = 1.20

The crucial part is missing from your question: how is Statistics#toString implemented? (Or whichever method generates the string output for your statistics class.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文