DecimalFormat

Discussion in 'Java' started by Johnny, Oct 25, 2003.

  1. Johnny

    Johnny Guest

    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 ;)
    Johnny, Oct 25, 2003
    #1
    1. Advertising

  2. Johnny

    VisionSet Guest

    "Johnny" <> wrote in message
    news:3f9af98e$0$13811$...
    > 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.]

    --
    Mike W
    VisionSet, Oct 26, 2003
    #2
    1. Advertising

  3. In article <3f9af98e$0$13811$>, on Sun, 26 Oct 2003
    01:30:34 +0300, "Johnny" <> wrote:

    | 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 />

    --
    David Postill
    David Postill, Oct 26, 2003
    #3
  4. Johnny

    Roedy Green Guest

    On Sun, 26 Oct 2003 01:30:34 +0300, "Johnny" <> wrote or
    quoted :

    >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.


    http://mindprod.com/converter.html see double to String.

    See also http://mindprod.com/jgloss/currency.html

    --
    Canadian Mind Products, Roedy Green.
    Coaching, problem solving, economical contract programming.
    See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
    Roedy Green, Oct 26, 2003
    #4
  5. VisionSet wrote:
    at 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.


    Then you were doing something wrong. Most European countries (definitely
    Germany, France, Belgium and the Netherlands) use a decimal comma.
    Michael Borgwardt, Oct 26, 2003
    #5
  6. VisionSet wrote:
    > "Johnny" <> wrote in message
    > news:3f9af98e$0$13811$...
    >
    >>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


    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
    Mark Thornton, Oct 26, 2003
    #6
  7. Johnny

    VisionSet Guest

    "Michael Borgwardt" <> wrote in message
    news:bngc8c$106l0r$-berlin.de...
    > VisionSet wrote:
    > at 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.

    >
    > 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.

    --
    Mike W
    VisionSet, Oct 26, 2003
    #7
  8. Johnny

    VisionSet Guest

    "Mark Thornton" <> wrote in message
    news:dVOmb.1644$...
    > VisionSet wrote:
    > >
    > >>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

    >
    > 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.

    --
    Mike W
    VisionSet, Oct 26, 2003
    #8
  9. VisionSet wrote:

    > "Mark Thornton" <> wrote in message
    > news:dVOmb.1644$...
    >
    >>VisionSet wrote:
    >>
    >>>>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

    >>
    >>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.
    >


    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
    Mark Thornton, Oct 26, 2003
    #9
  10. Johnny

    A Dahlman Guest

    Johnny wrote:
    >
    > 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 Dahlman, Oct 27, 2003
    #10
  11. Johnny

    A Dahlman Guest

    Johnny wrote:
    >
    > 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.
    A Dahlman, Oct 27, 2003
    #11
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Timo Nentwig

    Padding for DecimalFormat?

    Timo Nentwig, Jan 5, 2004, in forum: Java
    Replies:
    3
    Views:
    4,488
    Tony Dahlman
    Feb 12, 2004
  2. Cliff
    Replies:
    5
    Views:
    530
    Roedy Green
    May 12, 2004
  3. Adam Lipscombe
    Replies:
    2
    Views:
    769
    Virgil Green
    Nov 29, 2004
  4. Mark Sizzler
    Replies:
    1
    Views:
    1,407
    Roland
    Feb 21, 2005
  5. Jim Cobban
    Replies:
    6
    Views:
    1,846
    Roedy Green
    Sep 12, 2005
Loading...

Share This Page