DecimalFormat.applyPattern("#.##0,00") throwing an exception
Asked Answered
I

2

5

For the code:

DecimalFormat df = (DecimalFormat) NumberFormat.getNumberInstance(locale);
df.applyPattern("#.##0,00");

And it throws an IllegalArgumentException with a message of Malformed pattern "#.##0,00"

What is wrong with this?

Edit: I want to use the . as a thousands separator and , as the decimal. I know that's ass-backwards but have a case where that's the format they want.

Ilario answered 24/2, 2017 at 21:26 Comment(5)
My guess is that you cannot have , after ., see docs.oracle.com/javase/8/docs/api/java/text/DecimalFormat.htmlCreekmore
The comma is the grouping separator. The dot is the decimal separator. You used one instead of the other (and vice-versa).Zweig
try df.applyPattern("#,##0.00"); it will workNippy
@JBNizet - first off thank you for answering. I just added an edit, I need the number to use . as the thousand separator and , as the decimal. So they are switched on purpose.Ilario
To use the comma as decimal separator, the pattern stays the same. But the decimal format symbols are modified to use the comma as decimal separator. Or you just use the French (or other european) locale to get your NumberFormat (orDecimalFormatSymbols) instance, where that is the default.Zweig
S
13

You need to use the pattern "#,##0.##" to indicate where you want to decimal separator and the thousand seperator. The Locale you use when using this DecimalFormat will then determine if the decimal separator is a . or , -- the pattern should NOT be changed.

If you are not getting the correct separators, you will need to use a different Locale.

Stig answered 25/2, 2017 at 8:3 Comment(1)
Why didnt they pick !and | to make it clear that these are not representations of commas or dots but rather decimal and grouping sepparators. Now it looks like you are forced to use the US standard and give it a Locale to adapt to it. I thought i could just skip the localized part.....Extracellular
A
3

You do not have to use a different Locale.

You can use the DecimalFormatSymbols class to change the symbols that appear in the formatted numbers produced by the format method. These symbols include the decimal separator, the grouping separator, the minus sign, and the percent sign, among others.

The next example demonstrates the DecimalFormatSymbols class by applying a strange format to a number. The unusual format is the result of the calls to the setDecimalSeparator, setGroupingSeparator, and setGroupingSize methods.

DecimalFormatSymbols unusualSymbols = new DecimalFormatSymbols(currentLocale);
unusualSymbols.setDecimalSeparator('|');
unusualSymbols.setGroupingSeparator('^');

String strange = "#,##0.###";
DecimalFormat weirdFormatter = new DecimalFormat(strange, unusualSymbols);
weirdFormatter.setGroupingSize(4);

String bizarre = weirdFormatter.format(12345.678);
System.out.println(bizarre);

When run, this example prints the number in a bizarre format:

1^2345|678

Taken from: https://docs.oracle.com/javase/tutorial/i18n/format/decimalFormat.html

Assistance answered 26/7, 2021 at 21:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.