Results 1 to 5 of 5
  1. #1
    sabarooni's Avatar
    sabarooni is offline Member
    Join Date
    Nov 2011
    Posts
    19
    Rep Power
    0

    Lightbulb Help with decimals

    Hi I am trying to write a program which works out a bunch of stuff, and I want the final answer to be given to 2 decimal places Here is an excerpt from the code, the bit which I need to be given to 2 decimal places is the last two lines.

    I used 2 classes, om is a copy of another class which contains formulae and stuff

    Thanks for the help!

    System.out.print ("volume cost: ") ;
    System.out.println (""+length+" x "+width+" x "+height+" x "+om.multiplier+" =" + om.volumecost (length, width, height, om.multiplier)) ;
    System.out.print ("overhead cost: ") ;
    System.out.println (""+length+" x "+width+" / "+om.divider+" =" + om.overheadcost (length, width, om.divider)) ;
    System.out.print ("total cost: ") ;
    System.out.println (""+om.volumecost (length, width, height, om.multiplier)+" x "+om.overheadcost (length, width, om.divider)+" + "+om.officecharge+" =" + om.totalcost (om.volumecost (length, width, height, om.multiplier), om.overheadcost (length, width, om.divider), om.officecharge)) ;

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,271
    Blog Entries
    7
    Rep Power
    24

    Default Re: Help with decimals

    The PrintStream in the System class has a printf( ... ) method; the method uses a DecimalFormat object to do the actual formatting. The API documentation tells you all about it ...

    kind regards,

    Jos
    The only person who got everything done by Friday was Robinson Crusoe.

  3. #3
    sabarooni's Avatar
    sabarooni is offline Member
    Join Date
    Nov 2011
    Posts
    19
    Rep Power
    0

    Default Re: Help with decimals

    Quote Originally Posted by JosAH View Post
    The PrintStream in the System class has a printf( ... ) method; the method uses a DecimalFormat object to do the actual formatting. The API documentation tells you all about it ...

    kind regards,

    Jos

    Hi sorry I should have said this before, but I already tried using the printf function, but I am unclear on where I am supposed to put the "%.2d" , all the ways in which I tried it didn't work. Also could you possibly tell me where I can find these API documents? is it this?

    Java Platform SE 6

    thanks

  4. #4
    sabarooni's Avatar
    sabarooni is offline Member
    Join Date
    Nov 2011
    Posts
    19
    Rep Power
    0

    Default Re: Help with decimals

    Hi i probably should have said before that I already tried to use the printf method, but I am unsure of where to put the "%.2d" , in the coding. Also could you possibly show me where to find this API documentation? is it this?

    Java Platform SE 6

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    28

    Default Re: Help with decimals

    I always thought that printf didn't user or borrow from DecimalFormat but rather from the java.util.Formatter class. I'd look at its API which you can find here: Formatter API

Similar Threads

  1. Need help with truncating decimals?
    By akeni in forum New To Java
    Replies: 3
    Last Post: 10-05-2011, 12:31 AM
  2. Help with rounding decimals
    By university123 in forum New To Java
    Replies: 2
    Last Post: 10-04-2010, 10:28 PM
  3. BIG decimals
    By xael in forum New To Java
    Replies: 17
    Last Post: 10-04-2010, 10:14 AM
  4. Decimals in java help!!
    By Gold in forum New To Java
    Replies: 3
    Last Post: 12-10-2009, 12:25 AM
  5. get more decimals?!?! please help!
    By michcio in forum New To Java
    Replies: 7
    Last Post: 05-22-2008, 10:26 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
  •