How to get the simplest NumberFormats

P

Philipp

Hello
I would like to format numbers (doubles) for display. I want to use the
methods NumberFormat.setMinimumFractionDigits() for rounding, but I am
annoyed by the Locale.
How can I get rid of the Locale formatting and get back a simple number
format (with a dot as decimal separator).

Expected: 1234567.89
US: 1,234,567.89
My Own Locale: 1'234'567.89

How to get rid of all these ' and , ?

Thanks Phil
 
R

Robert Klemme

Hello
I would like to format numbers (doubles) for display. I want to use the
methods NumberFormat.setMinimumFractionDigits() for rounding, but I am
annoyed by the Locale.
How can I get rid of the Locale formatting and get back a simple number
format (with a dot as decimal separator).

Expected: 1234567.89
US: 1,234,567.89
My Own Locale: 1'234'567.89

How to get rid of all these ' and , ?

Thanks Phil

Please read the API docs. IIRC you can set a Locale on creation (or
later). You can also explicitly set the formatting symbols.

Kind regards

robert
 
P

Philipp

Philipp said:
Hello
I would like to format numbers (doubles) for display. I want to use the
methods NumberFormat.setMinimumFractionDigits() for rounding, but I am
annoyed by the Locale.
How can I get rid of the Locale formatting and get back a simple number
format (with a dot as decimal separator).

Expected: 1234567.89
US: 1,234,567.89
My Own Locale: 1'234'567.89

How to get rid of all these ' and , ?

Ok I got my solution. Sorry for the noise.

myFormat = new DecimalFormat("##0.#####E0");

does what I want (which is not the example I wrote above, but I know that).

But I can't understand why in the JavaDoc (at
http://java.sun.com/j2se/1.4.2/docs/api/java/text/DecimalFormat.html)
they say not to instanciate this class directly but to use the factory
method from NumberFormat.

I can't apply a pattern if I don't know if it is a DecimalFormat. In the
doc they say to do this:
NumberFormat f = NumberFormat.getInstance(loc);
if (f instanceof DecimalFormat) {
((DecimalFormat) f).setDecimalSeparatorAlwaysShown(true);
}

but what do you do if "f" is not an instanceOf DecimalFormat? Are you
suppose to die of a sudden death?

Phil
 

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

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top