# Thread: Hey Guys I need some help with rounding...

1. Member
Join Date
Nov 2009
Location
California
Posts
55
Rep Power
0

## 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. Member
Join Date
Nov 2009
Location
California
Posts
55
Rep Power
0
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. I wouldn't use an if statement. I would use Math.round.

4. Member
Join Date
Nov 2009
Location
California
Posts
55
Rep Power
0
oh yeah I should have check the math functions...

thanks!

5. ## 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. Senior Member
Join Date
Aug 2009
Posts
294
Rep Power
0
those parantessed around d isnt required tho, I think..

7. 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. Originally Posted by elektronika
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);
}```

9. 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

#### Posting Permissions

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