decimalformat

Discussion in 'Java' started by (-Peter-), Feb 9, 2008.

  1. (-Peter-)

    (-Peter-) Guest

    Hi..

    Can anybody help me how to print some output in a beautiful way?

    I want to have the decimal format 0,### ALWAYS..

    That is, if I for example have a double = 0,1 I want this to be
    printed as 0,100, and not 0,1..

    How do I force java to print the zeros?

    /Peter
    (-Peter-), Feb 9, 2008
    #1
    1. Advertising

  2. (-Peter-)

    Eric Sosman Guest

    (-Peter-) wrote:
    > Hi..
    >
    > Can anybody help me how to print some output in a beautiful way?
    >
    > I want to have the decimal format 0,### ALWAYS..
    >
    > That is, if I for example have a double = 0,1 I want this to be
    > printed as 0,100, and not 0,1..


    NumberFormat fmt = new DecimalFormat("0.000");
    System.out.println(fmt.format(0.1));

    or

    NumberFormat fmt = NumberFormat.getInstance();
    fmt.setMinimumFractionDigits(3);
    fmt.setMaximumFractionDigits(3);
    System.out.println(fmt.format(0.1));

    --
    Eric Sosman
    lid
    Eric Sosman, Feb 9, 2008
    #2
    1. Advertising

  3. (-Peter-)

    (-Peter-) Guest

    On 9 Feb., 17:01, Eric Sosman <> wrote:
    > (-Peter-) wrote:
    > > Hi..

    >
    > > Can anybody help me how to print some output in a beautiful way?

    >
    > > I want to have the decimal format 0,### ALWAYS..

    >
    > > That is, if I for example have a double = 0,1 I want this to be
    > > printed as 0,100, and not 0,1..

    >
    > NumberFormat fmt = new DecimalFormat("0.000");
    > System.out.println(fmt.format(0.1));
    >
    > or
    >
    > NumberFormat fmt = NumberFormat.getInstance();
    > fmt.setMinimumFractionDigits(3);
    > fmt.setMaximumFractionDigits(3);
    > System.out.println(fmt.format(0.1));
    >
    > --
    > Eric Sosman
    >


    Thank you very much :)

    /Peter
    (-Peter-), Feb 9, 2008
    #3
  4. (-Peter-)

    Stefan Ram Guest

    "(-Peter-)" <> writes:
    >0,1 I want this to be printed as 0,100, and not 0,1..


    public class Main
    { public static void main( final java.lang.String[] args )
    { java.lang.System.out.printf( "%.03f%n", 0.1 ); }}

    0,100
    Stefan Ram, Feb 9, 2008
    #4
    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,851
    A Dahlman
    Oct 27, 2003
  2. Timo Nentwig

    Padding for DecimalFormat?

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

Share This Page