简体   繁体   English

DecimalFormat“模式中有多个小数点分隔符”

[英]DecimalFormat “Multiple decimal separators in pattern”

I want to format decimal values as a currency value: 1234.56d should become "1.234,56" (this format is used in some European countries). 我想将十进制值格式化为货币值:1234.56d应该变成“ 1.234,56”(此格式在某些欧洲国家中使用)。 I'm using the following pattern to format decimal values: 我正在使用以下模式来格式化十进制值:

final DecimalFormat df = new DecimalFormat("###.###.###,00");
final String formatted = df.format(1234.56d);
System.out.println(formatted);

In fact I'm using SuperCSV 's class FmtNumber , but that doesn't matter, because the pattern syntax is the same: new FmtNumber("###.###.###,00"); 其实我使用SuperCSV的类FmtNumber ,但这并不重要,因为该模式的语法是一样的:新FmtNumber(‘### ### ###,00。’)

What I get is an IllegalArgumentException with the message "Multiple decimal separators in pattern". 我得到的是一个IllegalArgumentException消息“模式中有多个小数点分隔符”。

I understand that there can be only one decimal separator. 我知道只能有一个十进制分隔符。 But in my case it should be the sign ",". 但就我而言,它应该是符号“,”。 And "," appears only one time in my pattern. 在我的模式中,“,”仅出现一次。

I searched the Web and StackOverflow for this exception message, but I found no helpful answers. 我在Web和StackOverflow上搜索了此异常消息,但没有找到有用的答案。

What's my error? 我怎么了

From the javadoc in a format string . javadoc中输入格式字符串. is always the decimal separator and , is always a grouping separator. 始终是小数点分隔符,并且始终是分组分隔符。

Its actual representation in the formatted String is given by the locale of the formatter instance. 它在格式化的String中的实际表示形式由格式化程序实例的语言环境给出。

For example 例如

final DecimalFormat decimalFormat = new DecimalFormat("###,###,##0.00", DecimalFormatSymbols.getInstance(Locale.ENGLISH));

If you look at the javadoc for FmtNumber is clearly says "using the DecimalFormat class and the default locale" - so you need to have the correct default Locale for this to work. 如果您查看FmtNumberjavadoc ,显然会说“使用DecimalFormat类和默认语言环境”-因此,您需要具有正确的默认Locale才能正常工作。

You should to format with culture, not custom strings. 您应该使用区域性而不是自定义字符串进行格式化。 Just use: 只需使用:

decimalNumber.ToString("C2", CultureInfo.CreateSpecificCulture("fr-FR"));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM