Results 1 to 10 of 10
  1. #1
    lynxbci is offline Member
    Join Date
    Aug 2011
    Posts
    2
    Rep Power
    0

    Default Rounding calculations

    Hi,

    I have a formula that peforms a calculation, and as the inputs are double, the calc is too.
    Java Code:
    private double calcpoints(float input_pr, float input_ca, float input_fa, float input_fi) {
    		return ((input_pr / 10.9) + (input_ca / 9.2) + (input_fa / 3.9) + (input_fi / 35));
    However, this return 7.8443242 and i want it to return 8 (rounded value)
    How do i do this please?

    thanks

  2. #2
    Fabken is offline Member
    Join Date
    Nov 2010
    Posts
    18
    Rep Power
    0

    Default

    Hello Lynxbci, there is a round method in the java.lang.math library.

    Fabrice

  3. #3
    Fabken is offline Member
    Join Date
    Nov 2010
    Posts
    18
    Rep Power
    0

    Default

    float test = (float) 2.45;
    long testRound= Math.round(test);

    this is how you use it.

  4. #4
    lynxbci is offline Member
    Join Date
    Aug 2011
    Posts
    2
    Rep Power
    0

    Default

    Thanks very much all working ok

  5. #5
    RPFeltz is offline Member
    Join Date
    Aug 2011
    Posts
    4
    Rep Power
    0

    Default

    Perhaps converting it to an int value works too.
    double test = 7.82562;
    int rounded = (int) test;

  6. #6
    R-J
    R-J is offline Member
    Join Date
    Aug 2011
    Posts
    11
    Rep Power
    0

    Default

    Quote Originally Posted by RPFeltz View Post
    Perhaps converting it to an int value works too.
    double test = 7.82562;
    int rounded = (int) test;
    That will round everything between a 7 and an 8 to a 7.

  7. #7
    RPFeltz is offline Member
    Join Date
    Aug 2011
    Posts
    4
    Rep Power
    0

    Default

    What about this:
    double test = 7.82562;
    int rounded = (int) (test + 0.5);

  8. #8
    R-J
    R-J is offline Member
    Join Date
    Aug 2011
    Posts
    11
    Rep Power
    0

    Default

    That works unless the number is negative.

  9. #9
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    In general it's better to use what the libraries provide for you. Casting to an integer simply truncates the decimal portion. Like rj said, if x is -.6 and you did (int)(x+.5) you will still get 0.

  10. #10
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Or perhaps better would be to not round at all, but format the output into a nicely formatted and rounded String representation when this is needed. This can easily be done with a DecimalFormat object or String.format(...) method call.

Similar Threads

  1. update text output (eg. jLabel) during calculations
    By kosmo76 in forum AWT / Swing
    Replies: 2
    Last Post: 04-06-2011, 10:30 PM
  2. Thread calculations, and another thread to use the calculations
    By phil128 in forum Threads and Synchronization
    Replies: 4
    Last Post: 12-12-2010, 04:32 PM
  3. [SOLVED] Simple Calculations in Java
    By fullmetaljacket in forum New To Java
    Replies: 9
    Last Post: 05-19-2009, 03:19 AM
  4. decimal calculations?
    By arnab321 in forum CLDC and MIDP
    Replies: 5
    Last Post: 11-19-2008, 03:36 AM
  5. Need help rounding. =/
    By yo1mcool in forum New To Java
    Replies: 1
    Last Post: 10-07-2008, 05:02 AM

Posting Permissions

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