Results 1 to 5 of 5
Like Tree1Likes
  • 1 Post By DarrylBurke

Thread: How do you format to two decimal places with println()?

  1. #1
    EscSequenceAlpha is offline Senior Member
    Join Date
    Apr 2012
    Posts
    115
    Rep Power
    0

    Default How do you format to two decimal places with println()?

    ok so I'm just about finished with chapter 3 of deitel how to program, and they've given me loads of tasks which is fun.

    however the book prefers to use printf (using that technique you can format to two decimal places with %.2f)

    what about if you like using println?

    I found java.text.DecimalFormat.(#.##) then format(double)

    but the problem with this is it will round down to two decimal places, however if you dont have two decimal places (say 31.4) then it will remain 31.4 and will not round up to 31.40. so it cant be good with currency...

    is there an easy way to format using println? or is this what printf is for.

    thanks (I'm always frightened I'm asking stupid questions, but I like this forum because it says new to java so maybe I can ask stupid questions)

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,419
    Rep Power
    20

    Default Re: How do you format to two decimal places with println()?

    String#format(...) uses the same formatting codes as printf.

    db
    EscSequenceAlpha likes this.
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    EscSequenceAlpha is offline Senior Member
    Join Date
    Apr 2012
    Posts
    115
    Rep Power
    0

    Default Re: How do you format to two decimal places with println()?

    Thanks

    I tried

    + String.format("%2f", itemPrice)

    but somehow it turned 4.4(itemPrice) into 4.400000 lol oh java, you crazy

  4. #4
    EscSequenceAlpha is offline Senior Member
    Join Date
    Apr 2012
    Posts
    115
    Rep Power
    0

    Default Re: How do you format to two decimal places with println()?

    actually can someone tell me how to use this method properly, I keep getting lots of 0s when I do this.

  5. #5
    EscSequenceAlpha is offline Senior Member
    Join Date
    Apr 2012
    Posts
    115
    Rep Power
    0

    Default Re: How do you format to two decimal places with println()?

    forgot the . ok nevermind

    thanks for the method

Similar Threads

  1. round to two decimal places
    By javaMike in forum New To Java
    Replies: 3
    Last Post: 12-24-2011, 02:01 AM
  2. Replies: 7
    Last Post: 09-14-2011, 03:06 PM
  3. 3 Decimal Places
    By benhawk in forum New To Java
    Replies: 7
    Last Post: 11-02-2010, 09:28 PM
  4. [SOLVED] Decimal Places - Is this possible?
    By dbashby in forum New To Java
    Replies: 4
    Last Post: 04-08-2009, 07:49 PM
  5. Capping decimal places
    By Rageagainst20 in forum New To Java
    Replies: 1
    Last Post: 12-20-2007, 09:28 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •