十进制格式化程序无法解析为正确的形式?
我正在制作一个 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我未能使用
DecimalFormat
来将我的变量解析为两位小数。但是,以下解决方案也可以正常工作:where
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:where
is a
double
将双精度型格式化为字符串,然后再次将其解析为双精度型是没有意义的。根据您生成字符串的方式,尾随零将被删除。
如果需要两位小数,可以使用
String#format
:您的问题缺少关键部分:如何
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
:The crucial part is missing from your question: how is
Statistics#toString
implemented? (Or whichever method generates the string output for your statistics class.