[英]java.lang.IllegalArgumentException: Multiple decimal separators in pattern
I want to format a number using a pattern in Java.我想使用 Java 中的模式格式化数字。 I am getting error if the pattern has more than one decimal point.
如果模式有多个小数点,我会收到错误消息。 Here is the code:
这是代码:
DecimalFormatSymbols symbols = new DecimalFormatSymbols();
symbols.setDecimalSeparator(',');
DecimalFormat df = new DecimalFormat();
df.setDecimalFormatSymbols(symbols);
df.applyPattern("#.###.###.###,##");
String formattedValue = df.format("123456789");
I was testing it and it seems like the apply pattern needs a "," as grouping separator despite the one selected with the DecimalFormatSymbols.我正在测试它,似乎应用模式需要一个“,”作为分组分隔符,尽管使用 DecimalFormatSymbols 选择了一个。 You can customize the result setting the grouping separator in the DecimalFormatSymbols as well.
您也可以在 DecimalFormatSymbols 中自定义设置分组分隔符的结果。 This is the code I tested in a Unit Test
这是我在单元测试中测试的代码
DecimalFormatSymbols symbols = new DecimalFormatSymbols();
symbols.setDecimalSeparator(',');
symbols.setGroupingSeparator('.');
DecimalFormat df = new DecimalFormat();
df.setDecimalFormatSymbols(symbols);
df.applyPattern("#,###,###,###.##");
Assertions.assertEquals("123.456.789,25", df.format(123456789.25));
And use a number in the df.format argument并在 df.format 参数中使用数字
Add the DecimalFormatSymbols#setGroupingSeparator('.') method to your code, for example:将DecimalFormatSymbols#setGroupingSeparator('.')方法添加到您的代码中,例如:
DecimalFormatSymbols symbols = new DecimalFormatSymbols();
symbols.setDecimalSeparator(',');
symbols.setGroupingSeparator('.'); // Add This.
but also utilize a format that is specific to your system's current locale, for example:但也可以使用特定于系统当前语言环境的格式,例如:
df.applyPattern("#,###.##");
Notice that you really don't need to go as deep as you do with the format pattern.请注意,您确实不需要像使用格式模式那样深入 go。 What is shown above will suffice.
上面显示的就足够了。 Numerical groups always contain 3 digits, you just need to specify that you want a separator character there
数字组始终包含 3 位数字,您只需要指定要在其中使用分隔符
Also, use a number value rather than a numerical string, for example:此外,请使用数字值而不是数字字符串,例如:
int value = 123456789; // or 123_456_789
String formattedValue = df.format(value);
Altogether, this should work:总而言之,这应该有效:
DecimalFormatSymbols symbols = new DecimalFormatSymbols();
symbols.setDecimalSeparator(',');
symbols.setGroupingSeparator('.'); // Added this...
DecimalFormat df = new DecimalFormat();
df.setDecimalFormatSymbols(symbols);
df.applyPattern("#,###.##"); // Note the format pattern used.
int value = 123456789;**strong text** // Use a number variable.
String formattedValue = df.format(value);
System.out.println(formattedValue);
Output to console: Output 到控制台:
123.456.789
Some countries like Italy, Spain, and Norway for example use this number format and with this in mind you could do this:例如,意大利、西班牙和挪威等一些国家/地区使用这种数字格式,考虑到这一点,您可以这样做:
DecimalFormatSymbols symbols = new DecimalFormatSymbols(java.util.Locale.ITALIAN);
DecimalFormat df = new DecimalFormat("#,###.##", symbols);
int value = 123456789; // or 123_456_789
String formattedValue = df.format(value);
System.out.println(formattedValue);
Output to console: Output 到控制台:
123.456.789
Notice the Locale added to the DecimalFormatSymbols constructor (Locale.ITALIAN).请注意添加到DecimalFormatSymbols构造函数 (Locale.ITALIAN) 的区域设置。 Also notice the actual format.
还要注意实际的格式。 Use the format for your System's specific locale.
使用系统特定语言环境的格式。 Also notice that a int variable type containing a literal number is used instead of a numerical string.
另请注意,使用包含文字数字的int变量类型而不是数字字符串。
Try it with a double type (floating point) value:尝试使用双精度类型(浮点)值:
DecimalFormatSymbols symbols = new DecimalFormatSymbols(java.util.Locale.ITALIAN);
DecimalFormat df = new DecimalFormat("#,###.##", symbols);
double value = 1234567.89d;
String formattedValue = df.format(value);
System.out.println(formattedValue);
Output to console: Output 到控制台:
1.234.567,89
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.