How to suppress final 0 with DecimalFormat?

Discussion in 'Java' started by Adam Lipscombe, Nov 29, 2004.

  1. Folks,

    I need to output a blank if a double is 0.0;

    If I use DecimalFormat to format a zero double I get a single "0" output.
    i.e.

    double d = 0.0;
    DecimalFormat dF = new DecimalFormat("#.##");
    System.out.println(dF.format(d));

    gives "0".


    How do I get rid of the zero?


    TIA -Adam
     
    Adam Lipscombe, Nov 29, 2004
    #1
    1. Advertising

  2. Adam Lipscombe wrote:

    > Folks,
    >
    > I need to output a blank if a double is 0.0;
    >
    > If I use DecimalFormat to format a zero double I get a single "0" output.
    > i.e.
    >
    > double d = 0.0;
    > DecimalFormat dF = new DecimalFormat("#.##");
    > System.out.println(dF.format(d));
    >
    > gives "0".
    >
    >
    > How do I get rid of the zero?


    Write your own NumberFormat implementation: what you want is not
    possible with DecimalFormat. Make your NumberFormat's parse() method
    transform blanks back into zeroes, for otherwise it will be broken. Do
    make sure you appreciate the implications for error detection during
    parsing.


    John Bollinger
     
    John C. Bollinger, Nov 29, 2004
    #2
    1. Advertising

  3. Adam Lipscombe

    Virgil Green Guest

    "Adam Lipscombe" <> wrote in message
    news:...
    > Folks,
    >
    > I need to output a blank if a double is 0.0;
    >
    > If I use DecimalFormat to format a zero double I get a single "0" output.
    > i.e.
    >
    > double d = 0.0;
    > DecimalFormat dF = new DecimalFormat("#.##");
    > System.out.println(dF.format(d));
    >
    > gives "0".
    >
    >
    > How do I get rid of the zero?


    System.out.println(d == 0 ? "" : dF.format(d));

    - Virgil
     
    Virgil Green, Nov 29, 2004
    #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. Johnny

    DecimalFormat

    Johnny, Oct 25, 2003, in forum: Java
    Replies:
    10
    Views:
    7,866
    A Dahlman
    Oct 27, 2003
  2. Timo Nentwig

    Padding for DecimalFormat?

    Timo Nentwig, Jan 5, 2004, in forum: Java
    Replies:
    3
    Views:
    4,528
    Tony Dahlman
    Feb 12, 2004
  3. Cliff
    Replies:
    5
    Views:
    547
    Roedy Green
    May 12, 2004
  4. Mark Sizzler
    Replies:
    1
    Views:
    1,444
    Roland
    Feb 21, 2005
  5. Jim Cobban
    Replies:
    6
    Views:
    1,876
    Roedy Green
    Sep 12, 2005
Loading...

Share This Page