6 Replies Latest reply on Dec 20, 2006 11:48 PM by 807599

# decide the number o decimal houses in a decimal number

hi,
I want to print a double but if I use toString i'm not able to do what I want that is something like this in c language:
``printf("%2f",var);``
which is if I have 1,42352424554 I want to print just 1.42.

how can I do that?

Thanks
• ###### 1. Re: decide the number o decimal houses in a decimal number
Have a look at the DecimalFormat class or read all about the
printf method available in Java 1.5 or later.

kind regards,

Jos

ps. I like the phrase 'decimal houses'.
• ###### 2. Re: decide the number o decimal houses in a decimal number
You can also use String.format() method in 1.5 and up

~Tim
• ###### 3. Re: decide the number o decimal houses in a decimal number
the "decimal houses" expressions is because I don't remember how to say it in english and so I used the portugueses expression that is "casas decimais"

Thanks
• ###### 4. Re: decide the number o decimal houses in a decimal number
the "decimal houses" expressions is because I don't
remember how to say it in english and so I used the
portugueses expression that is "casas decimais"
No hard feelings; I simply liked the "decimal houses" phrase; and more
important: I understood it ;-)
Thanks
You're welcome of course.

kind regards,

Jos
• ###### 5. Re: decide the number o decimal houses in a decimal number
the "decimal houses" expressions is because I don't
remember how to say it in english and so I used the
portugueses expression that is "casas decimais"

Thanks
Decimal places.
• ###### 6. Re: decide the number o decimal houses in a decimal number
I want to print a double but if I use toString i'm
not able to do what I want that is something like
this in c language:
``printf("%2f",var);``
which is if I have 1,42352424554 I want to print just
1.42
Very similar to the C approach -
``````        double v = 1.42352424554;
System.out.printf("%1.2f", v);``````
Check the Javadoc of Formatter for details of this.