How to get the simplest NumberFormats

Discussion in 'Java' started by Philipp, Mar 12, 2007.

  1. Philipp

    Philipp Guest

    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
     
    Philipp, Mar 12, 2007
    #1
    1. Advertising

  2. On 12.03.2007 12:11, Philipp wrote:
    > 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
     
    Robert Klemme, Mar 12, 2007
    #2
    1. Advertising

  3. Philipp

    Philipp Guest

    Philipp wrote:
    > 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
     
    Philipp, Mar 12, 2007
    #3
    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. walala
    Replies:
    3
    Views:
    10,227
    VhdlCohen
    Sep 14, 2003
  2. Brent Burkart
    Replies:
    5
    Views:
    798
    Jerry III
    Oct 16, 2003
  3. Bob
    Replies:
    0
    Views:
    401
  4. Quentin Huo
    Replies:
    3
    Views:
    466
    Patrice
    Oct 5, 2004
  5. Replies:
    3
    Views:
    146
    Joel VanderWerf
    Nov 16, 2006
Loading...

Share This Page