Results 1 to 9 of 9
  1. #1
    blueduiker is offline Member
    Join Date
    Nov 2009
    Location
    California
    Posts
    55
    Rep Power
    0

    Talking Hey Guys I need some help with rounding...

    Hi guys,

    So basically, for some reason my mind has gone blank in the middle of doing my project. For a part of this project I need to have the numbers if the tenths digit is 5 or greater round up. How would you recommend doing this using an if-statement?

  2. #2
    blueduiker is offline Member
    Join Date
    Nov 2009
    Location
    California
    Posts
    55
    Rep Power
    0

    Default

    guys i need to get this done by 11 otherwise my parents r gonna whip me w/ a belt!

    this is the final part to the project so please help

  3. #3
    CodesAway's Avatar
    CodesAway is offline Senior Member
    Join Date
    Sep 2009
    Location
    Texas
    Posts
    238
    Rep Power
    6

    Default

    I wouldn't use an if statement. I would use Math.round.
    CodesAway - codesaway.info
    writing tools that make writing code a little easier

  4. #4
    blueduiker is offline Member
    Join Date
    Nov 2009
    Location
    California
    Posts
    55
    Rep Power
    0

    Default

    oh yeah I should have check the math functions...

    thanks!

  5. #5
    elektronika's Avatar
    elektronika is offline Member
    Join Date
    Dec 2009
    Location
    Indonesia
    Posts
    17
    Rep Power
    0

    Default Optional Advice

    Or you can also use something very simple like this :

    Java Code:
    int a;
    float d = 0.121334;
    d = d + 0.5;
    a = (int)(d);
    then the a is the rounding of the d~~! (if d >= 0.5 it will become 1, else 0)

    Kindest Regards,




    Elektronika

  6. #6
    Addez is offline Senior Member
    Join Date
    Aug 2009
    Posts
    294
    Rep Power
    0

    Default

    those parantessed around d isnt required tho, I think..

  7. #7
    elektronika's Avatar
    elektronika is offline Member
    Join Date
    Dec 2009
    Location
    Indonesia
    Posts
    17
    Rep Power
    0

    Default

    Yes,,you can just do not use it..
    I just like to make things a bit fancy (it is just mystyle of programming),,

    Kindest Regards,




    Elektronika

  8. #8
    CodesAway's Avatar
    CodesAway is offline Senior Member
    Join Date
    Sep 2009
    Location
    Texas
    Posts
    238
    Rep Power
    6

    Default

    Quote Originally Posted by elektronika View Post
    Or you can also use something very simple like this :

    Java Code:
    int a;
    float d = 0.121334;
    d = d + 0.5;
    a = (int)(d);
    then the a is the rounding of the d~~! (if d >= 0.5 it will become 1, else 0)

    Your method only works for positive values. Instead of doing a cast to int, you should be taking the floor.

    Java Code:
    int a;
    float d = -.99f;
    d = d + 0.5f;
    a = (int)(d); // a is 0, but should be 1


    The Math.round method is a convenience method for adding .5 and taking the floor - which rounds the value to the nearest integer. Below is the Java source code for Math.round.
    Java Code:
    public static int round(float a) {
    	return (int)floor(a + 0.5f);
    }
    CodesAway - codesaway.info
    writing tools that make writing code a little easier

  9. #9
    elektronika's Avatar
    elektronika is offline Member
    Join Date
    Dec 2009
    Location
    Indonesia
    Posts
    17
    Rep Power
    0

    Default

    Thank you CodesAway..yes indeed mine is only for positive values..except you put another if..
    so if positive value then +0.5, if negative value then -0.5..easy huh?

    0.99 + 0.5 = 1.49 -> 1
    -0.99 - 0.5 = -1.49 -> -1

    But anyway the round method is there..so using it I think is better to use it :)

    Kindest Regards,




    Elektronika

Similar Threads

  1. rounding double to two decimal places
    By javaMike in forum Advanced Java
    Replies: 15
    Last Post: 03-10-2010, 12:04 AM
  2. Rounding
    By Moncleared in forum New To Java
    Replies: 2
    Last Post: 11-17-2009, 08:16 AM
  3. [SOLVED] Rounding off numbers
    By Manfizy in forum New To Java
    Replies: 4
    Last Post: 02-07-2009, 12:44 AM
  4. Need help rounding. =/
    By yo1mcool in forum New To Java
    Replies: 1
    Last Post: 10-07-2008, 05:02 AM
  5. Help with java Rounding
    By silvia in forum New To Java
    Replies: 1
    Last Post: 07-20-2007, 07:25 AM

Tags for this Thread

Posting Permissions

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