DecimalFormat with Locale problem

A

asafshec

hello,

Is this a bug in DecimalFormat, or that I am not using it correctly?

this is my code:
public static void main(String[] args)
{
// create German Locale
Locale locale = new Locale("de");
DecimalFormat format1 =
(DecimalFormat)DecimalFormat.getInstance(locale);
try {
DecimalFormat format2 = new
DecimalFormat(format1.toLocalizedPattern());
}
catch(Exception e) {
System.out.print(e);
}

and I get this Exception:
java.lang.IllegalArgumentException: Malformed pattern "#.##0,###"

I will really appriciate any help on this.
Thanks,
Asaf
 
R

Roedy Green

java.lang.IllegalArgumentException: Malformed pattern "#.##0,###"
You write the patterns as if you were in North America. Then if you
are in Sweden it will reverse the decimal and comma on use.
You would thus write that pattern as "#,##0.###"
 
O

Oliver Wong

asafshec said:
hello,

Is this a bug in DecimalFormat, or that I am not using it correctly?

this is my code:
public static void main(String[] args)
{
// create German Locale
Locale locale = new Locale("de");
DecimalFormat format1 =
(DecimalFormat)DecimalFormat.getInstance(locale);
try {
DecimalFormat format2 = new
DecimalFormat(format1.toLocalizedPattern());
}
catch(Exception e) {
System.out.print(e);
}

and I get this Exception:
java.lang.IllegalArgumentException: Malformed pattern "#.##0,###"

I will really appriciate any help on this.

From the Javadocs for the constructor DecimalFormat(string):

<javadoc>
Creates a DecimalFormat using the given pattern and the symbols for the
default locale.
</javadoc>

Therefore, the constructor expects the pattern to be in the default
locale, but you've supplied a pattern in the German locale, which is why
it's confused.

- Oliver
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top