DecimalFormat

J

Johnny

Hello,

What kind of mask should I use with DecimalFormat class if I would like to
get allways two digits after decimalpoint? E.g. if input is 100.1 then
result should be 100.10.

Then second questions is that people is USA are using
dot as a separator in decimal numbers. But in Northern
Europe it's comma. What is the most elegant way to
implement this point/comma localized difference when handling decimal
numbers?

Cheers,

It's snow in the ground here in Scandinavia now ;)
 
V

VisionSet

Johnny said:
Hello,

What kind of mask should I use with DecimalFormat class if I would like to
get allways two digits after decimalpoint? E.g. if input is 100.1 then
result should be 100.10.

DecimalFormat df = new DecimalFormat("0.00");

System.out.println(df.format(100.1)); // gives 100.10
Then second questions is that people is USA are using
dot as a separator in decimal numbers. But in Northern
Europe it's comma.

Not in the UK it isn't
What is the most elegant way to
implement this point/comma localized difference when handling decimal
numbers?

eh?
I've run the example code in java.text.DecimalFormat API, and can't see any
country that uses anything other than decimal point.

Have a look at that code anyway it should tell you what you want to know.

[IIRC a decimal point and full stop are different, but from an IT
perspective they are the same and are both the full stop. A true decimal
point ( · [alt 0183 on Windows]) hovers halfway up the line.]
 
D

David Postill

| Hello,
|
| What kind of mask should I use with DecimalFormat class if I would like to
| get allways two digits after decimalpoint? E.g. if input is 100.1 then
| result should be 100.10.
|
| Then second questions is that people is USA are using
| dot as a separator in decimal numbers. But in Northern
| Europe it's comma. What is the most elegant way to
| implement this point/comma localized difference when handling decimal
| numbers?

Have you read the Java Tutorial yet?

<http://java.sun.com/docs/books/tutorial/i18n/format/decimalFormat.html>

Answers both of your questions.

| Cheers,
|
| It's snow in the ground here in Scandinavia now ;)

Pretty cold here too in the UK. No snow yet though :)

<davidp />
 
M

Michael Borgwardt

VisionSet wrote:
at is the most elegant way to
eh?
I've run the example code in java.text.DecimalFormat API, and can't see any
country that uses anything other than decimal point.

Then you were doing something wrong. Most European countries (definitely
Germany, France, Belgium and the Netherlands) use a decimal comma.
 
M

Mark Thornton

VisionSet said:
DecimalFormat df = new DecimalFormat("0.00");

System.out.println(df.format(100.1)); // gives 100.10




Not in the UK it isn't

True, but then the OP did say they were in Scandinavia. From that point
of view the UK may not be in "Northern Europe"! The ISO standards also
prefer the comma form and they are used in the UK. As far as I can tell
the UK (and possibly Ireland) are the only European locales using '.' as
the decimal separator. In currencies, some countries use a currency
symbol as the divider (e.g. Portugal).

Mark Thornton
 
V

VisionSet

Michael Borgwardt said:
VisionSet wrote:
at is the most elegant way to

Then you were doing something wrong. Most European countries (definitely
Germany, France, Belgium and the Netherlands) use a decimal comma.

Yep, I was looking at the wrong part of the output.
 
V

VisionSet

Mark Thornton said:
True, but then the OP did say they were in Scandinavia. From that point
of view the UK may not be in "Northern Europe"!

True, but I always took Northern Europe to mean none Mediteranean.
 
M

Mark Thornton

VisionSet said:
True, but I always took Northern Europe to mean none Mediteranean.

So do I, but you never know. There are also many people here (UK) who
use "Europe" in a sense which doesn't include the UK. As in the old
joke: "Fog in the channel, Europe isolated!"

Mark Thornton
 
A

A Dahlman

Johnny said:
Hello,

What kind of mask should I use with DecimalFormat class if I would like to
get allways two digits after decimalpoint? E.g. if input is 100.1 then
result should be 100.10.

Then second questions is that people is USA are using
dot as a separator in decimal numbers. But in Northern
Europe it's comma. What is the most elegant way to
implement this point/comma localized difference when handling decimal
numbers?

Cheers,

It's snow in the ground here in Scandinavia now ;)

Using DecimalFormat and a mask already defeats Java's built-in international
localization features. Use NumberFormat.getInstance() or getCurrencyInstance()
instead. Americans, Canadians, Brits and Irishpersons should learn this habit,
too. The resulting object is still of type DecimalFormat, but it is localized,
depending on the location of the client that runs the program.

Usually it will not be necessary, but if you must, you can limit the decimal
places to 2 by using setMinimumFractionDigits( int ) and setMaximum...( int ),
which are part of the NumberFormat API.

Skål, Tony Dahlman

Hey, and in Austria, the first World Cup Slalom medals are being handed out.
We're in a hot spell (no rain since July in No. California, and fires raging
down south), but I can't wait for snow and skiing.
 
A

A Dahlman

Johnny said:
Hello,

What kind of mask should I use with DecimalFormat class if I would like to
get allways two digits after decimalpoint? E.g. if input is 100.1 then
result should be 100.10.

Then second questions is that people is USA are using
dot as a separator in decimal numbers. But in Northern
Europe it's comma. What is the most elegant way to
implement this point/comma localized difference when handling decimal
numbers?

Cheers,

It's snow in the ground here in Scandinavia now ;)


Using DecimalFormat and a mask already defeats Java's built-in international
localization features. Use NumberFormat.getInstance() or getCurrencyInstance()
instead. Americans, Canadians, Brits and Irishpersons should learn this habit,
too. The resulting object is still of type DecimalFormat, but it is localized,
depending on the location of the client that runs the program.

Usually it will not be necessary, but if you must, you can limit the decimal
places to 2 by using setMinimumFractionDigits( int ) and setMaximum...( int ),
which are part of the NumberFormat API.

Lots of hints on internationalizing your code are at:

http://pws.prserv.net/ad/programs/Programs.html

Skål, Tony Dahlman

Hey, and in Austria, the first World Cup Slalom medals are being handed out.
We're in a hot spell (no rain since July in No. California, and fires raging
down south), but I can't wait for snow and skiing.
 

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

Similar Threads


Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top