DecimalFormat在指定模式为点时仍然使用逗号分隔符

huangapple go评论58阅读模式
英文:

DecimalFormat uses comma separator even when the pattern specified is a dot

问题

最后一行失败并出现NumberFormatException,因为小数点被逗号分隔。可能的原因是您的本地设置或语言环境将小数点解释为逗号。您可以尝试以下解决方法:

  1. 更改本地设置或语言环境为使用小数点而不是逗号作为小数分隔符。

  2. 在代码中使用Locale来指定使用小数点。例如:

DecimalFormat df = new DecimalFormat("0.00", new DecimalFormatSymbols(Locale.US));

这将使用美国的本地设置,其中小数点是小数的分隔符。

请注意,这只是一个可能的解决方案,具体取决于您的环境和要求。

英文:

I have the below code for formatting a double to two decimal places;

DecimalFormat df = new DecimalFormat("0.00");
double monthlyVal = forecastReturnValue / months;
return Double.valueOf(df.format(monthlyVal));

The last line fails with a NumberFormatException because decimals are separated by a comma;

DecimalFormat在指定模式为点时仍然使用逗号分隔符

I am using windows OpenJDK8:

openjdk version "1.8.0_41"
OpenJDK Runtime Environment (build1.8.0_41-b04)
OpenJDK Client VM (build 25.40-b25, mixed mode)

What could be the cause?

答案1

得分: 1

DecimalFormat 仅用于格式化值。Double.valueOf 不知道格式,如果要解析这种字符串,必须使用 DecimalFormat

return df.parse(df.format(monthlyVal));
英文:

The DecimalFormat ist only for formating the value. Double.valueOf doesn't know the format and expect a . if you want to parse duch string you have to use DecimalFormat for parsing this value:

return df.parse(df.format(monthlyVal));

答案2

得分: 1

原因是Locale。默认情况下,您连接到系统上的Locale,它返回逗号作为小数分隔符。要更改使用DecimalFormat,执行以下操作:

DecimalFormatSymbols symbols = new DecimalFormatSymbols(Locale.US);
DecimalFormat df = new DecimalFormat("0.00", symbols);
double monthlyVal = forecastReturnValue / months;
return Double.valueOf(df.format(monthlyVal));

依我之见,我建议在这种情况下使用BigDecimal,因为它更精确,更不容易出错,并且更容易处理四舍五入、缩放等用途。

等价的方式可以是:

BigDecimal monthlyVal = BigDecimal.valueOf(20.0).divide(BigDecimal.valueOf(12), 2, RoundingMode.CEILING); // 2是标度。RoundingMode取决于您
System.out.println(monthlyVal);
英文:

The cause is the Locale. By default, you are connected to the Locale on your system, which return a decimal separator as ,. Using DecimalFormat, to change, do the following:

DecimalFormatSymbols symbols = new DecimalFormatSymbols(Locale.US);
DecimalFormat df = new DecimalFormat("0.00", symbols);
double monthlyVal = forecastReturnValue / months;
return Double.valueOf(df.format(monthlyVal));

IMHO, I'd recommend using BigDecimal for such use cases, as it is more accurate, less error prone, and handle easily usage of rounding, scaling and so on.

The equivalent could be:

BigDecimal monthlyVal = BigDecimal.valueOf(20.0).divide(BigDecimal.valueOf(12), 2, RoundingMode.CEILING); // 2 is the scale. RoundingMode is up to you
System.out.println(monthlyVal);

答案3

得分: 1

当你写new DecimalFormat("0.00");时,你没有指定分隔符类型。你只是指定了应该有一个分隔符。分隔符仍然会基于你的区域设置。

格式字符串使用"."作为小数点分隔符。

如果你想使用'.'作为小数点分隔符,你可以更改你的区域设置。

英文:

When you write new DecimalFormat("0.00"); you are not specifying the separator type. You are just specifying that there should be a separator. The separator will still be based on your locale.

Format Strings us "." as the decimal separator.

If you want to use a '.' you can change your locale.

huangapple
  • 本文由 发表于 2020年7月30日 18:20:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/63171113.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定