DecimalFormat

Discussion in 'Java' started by Roedy Green, Jul 29, 2010.

  1. Roedy Green

    Roedy Green Guest

    If you wanted to trim trailing 0s from a string representation of a
    number after the decimal point, can you do it directly with
    DecimalFormat, or do you need to do String operations on the result?
    --
    Roedy Green Canadian Mind Products
    http://mindprod.com

    You encapsulate not just to save typing, but more importantly, to make it easy and safe to change the code later, since you then need change the logic in only one place. Without it, you might fail to change the logic in all the places it occurs.
     
    Roedy Green, Jul 29, 2010
    #1
    1. Advertising

  2. In article <>,
    Roedy Green <> wrote:

    > If you wanted to trim trailing 0s from a string representation of a
    > number after the decimal point, can you do it directly with
    > DecimalFormat, or do you need to do String operations on the result?


    I'm pretty sure you'd have to convert the String to a Number, long or
    double before you could format() it.

    --
    John B. Matthews
    trashgod at gmail dot com
    <http://sites.google.com/site/drjohnbmatthews>
     
    John B. Matthews, Jul 29, 2010
    #2
    1. Advertising

  3. Roedy Green

    Eric Sosman Guest

    On 7/28/2010 10:23 PM, Roedy Green wrote:
    > If you wanted to trim trailing 0s from a string representation of a
    > number after the decimal point, can you do it directly with
    > DecimalFormat, or do you need to do String operations on the result?


    If you're starting with the numeric value (as opposed to an
    already-generated String), just use the '#' pattern character, as

    NumberFormat fmt = new DecimalFormat("#.##");

    This will format 1.20 as "1.2" and 3.45 as "3.45".

    --
    Eric Sosman
    lid
     
    Eric Sosman, Jul 29, 2010
    #3
  4. Roedy Green

    Roedy Green Guest

    On Thu, 29 Jul 2010 00:08:23 -0400, "John B. Matthews"
    <> wrote, quoted or indirectly quoted someone who
    said :

    >I'm pretty sure you'd have to convert the String to a Number, long or
    >double before you could format() it.


    I have it as a double. DecimalFormat.format gives me the String.

    My question is, is there any tricky pattern you can give it to trim
    trailing 000, or do you need to process the output of .format as a
    String?
    --
    Roedy Green Canadian Mind Products
    http://mindprod.com

    You encapsulate not just to save typing, but more importantly, to make it easy and safe to change the code later, since you then need change the logic in only one place. Without it, you might fail to change the logic in all the places it occurs.
     
    Roedy Green, Jul 29, 2010
    #4
  5. Roedy Green

    Roedy Green Guest

    On Thu, 29 Jul 2010 00:22:32 -0400, Eric Sosman
    <> wrote, quoted or indirectly quoted
    someone who said :

    >
    > NumberFormat fmt = new DecimalFormat("#.##");
    >
    >This will format 1.20 as "1.2" and 3.45 as "3.45".


    Any is there a way to trim .00 to get rid of the dot too, or is that
    the default behaviour?
    --
    Roedy Green Canadian Mind Products
    http://mindprod.com

    You encapsulate not just to save typing, but more importantly, to make it easy and safe to change the code later, since you then need change the logic in only one place. Without it, you might fail to change the logic in all the places it occurs.
     
    Roedy Green, Jul 29, 2010
    #5
  6. Roedy Green

    Eric Sosman Guest

    On 7/29/2010 1:52 AM, Roedy Green wrote:
    > On Thu, 29 Jul 2010 00:22:32 -0400, Eric Sosman
    > <> wrote, quoted or indirectly quoted
    > someone who said :
    >
    >>
    >> NumberFormat fmt = new DecimalFormat("#.##");
    >>
    >> This will format 1.20 as "1.2" and 3.45 as "3.45".

    >
    > Any is there a way to trim .00 to get rid of the dot too, or is that
    > the default behaviour?


    fmt.setDecimalSeparatorAlwaysShown(false);

    --
    Eric Sosman
    lid
     
    Eric Sosman, Jul 29, 2010
    #6
  7. In article <>,
    Roedy Green <> wrote:

    > On Thu, 29 Jul 2010 00:08:23 -0400, "John B. Matthews"
    > <> wrote, quoted or indirectly quoted someone who
    > said :
    >
    > >I'm pretty sure you'd have to convert the String to a Number, long or
    > >double before you could format() it.

    >
    > I have it as a double.


    Ah, you only mentioned string. Eric's answers are what you want.

    [...]
    --
    John B. Matthews
    trashgod at gmail dot com
    <http://sites.google.com/site/drjohnbmatthews>
     
    John B. Matthews, Jul 29, 2010
    #7
  8. Roedy Green

    Roedy Green Guest

    On Wed, 28 Jul 2010 22:52:19 -0700, Roedy Green
    <> wrote, quoted or indirectly quoted
    someone who said :

    >
    >Any is there a way to trim .00 to get rid of the dot too, or is that
    >the default behaviour?


    I found that .### works exactly as needed, dropping the dot for .000.
    Thanks.
    --
    Roedy Green Canadian Mind Products
    http://mindprod.com

    You encapsulate not just to save typing, but more importantly, to make it easy and safe to change the code later, since you then need change the logic in only one place. Without it, you might fail to change the logic in all the places it occurs.
     
    Roedy Green, Jul 29, 2010
    #8
    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,923
    A Dahlman
    Oct 27, 2003
  2. Timo Nentwig

    Padding for DecimalFormat?

    Timo Nentwig, Jan 5, 2004, in forum: Java
    Replies:
    3
    Views:
    4,667
    Tony Dahlman
    Feb 12, 2004
  3. Cliff
    Replies:
    5
    Views:
    564
    Roedy Green
    May 12, 2004
  4. Adam Lipscombe
    Replies:
    2
    Views:
    858
    Virgil Green
    Nov 29, 2004
  5. Mark Sizzler
    Replies:
    1
    Views:
    1,554
    Roland
    Feb 21, 2005
Loading...

Share This Page