Results 1 to 6 of 6

Thread: int division

  1. #1
    überfuzz is offline Member
    Join Date
    Feb 2011
    Posts
    43
    Rep Power
    0

    Default int division

    I'm not sure of how to work with int while I divide stuff...

    I just finished a program that ends up in a presentation of some statistics. There's an issue when I want to display the quota of two integers.

    Something like this
    Java Code:
    //figures derived in the program.
    int figureOne = 2;
    int figureTwo = 3;
    
    //in a print method
    System.out.println("The quota is: " + figureOne/figureTwo);
    Any simple fix to resolve this?

  2. #2
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    6

    Default

    Quote Originally Posted by überfuzz View Post

    Any simple fix to resolve this?
    One of them must be a double!
    Java Code:
    System.out.println("The quota is: " + figureOne/(double)figureTwo);

  3. #3
    überfuzz is offline Member
    Join Date
    Feb 2011
    Posts
    43
    Rep Power
    0

    Default

    Thank you, it works like a charm!

    Is there a way of limiting the number of decimals?

  4. #4
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    6

    Default

    Sure, e.g.
    Java Code:
    System.out.printf("The quota is: %.3f",  figureOne/(double)figureTwo);

  5. #5
    FlipPoker@gmail.com is offline Senior Member
    Join Date
    Mar 2011
    Posts
    103
    Rep Power
    0

    Default

    When you divide two integers, the result will be an integer. In other words, you will lose the decimal places. If you want to keep the decimal places, then cast one of them to double (as stated). The other value will get promoted to double and you will get a double result.

  6. #6
    überfuzz is offline Member
    Join Date
    Feb 2011
    Posts
    43
    Rep Power
    0

    Default

    Poker - I figure that one out. I've been messing about with type dynamic languages for to long, I guess.

    Thanks!

Similar Threads

  1. Issues with Division
    By KnotsaboutWood in forum New To Java
    Replies: 11
    Last Post: 01-31-2011, 03:32 AM
  2. big double division
    By grilo in forum Advanced Java
    Replies: 4
    Last Post: 10-11-2009, 10:07 PM
  3. Problem with division using doubles
    By chrismanahan in forum New To Java
    Replies: 3
    Last Post: 10-10-2009, 09:26 PM
  4. java division and decimal error
    By heartysnowy in forum New To Java
    Replies: 5
    Last Post: 10-07-2009, 04:57 PM
  5. how to discard remainder on division?
    By RobertF in forum New To Java
    Replies: 9
    Last Post: 03-13-2009, 12:20 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
  •