1. ## Rounding function

An assignment I'm currently doing required me to round of a float. I didn't know if there was a standard function for it or not, (if there is I couldn't find it) so I wrote my own.

Code:
```int round(float x){
int y = (int)x;
if((x - y) > 0.5f){
x = (float)++y;
}
else{
x = (float)y;
}
return (int)x;
}```
It works fine but I'm just seeing what people have to say about it. Maybe some tips on improvment.

2. why are you casting y to a float before assigning it to x? and why are you returning x?

why not just increment or decrement y and return it?

and the answer to your standard function question: kinda. ther's ceil(x) and floor(x). you can probably tell what they do from their names...

edit:
Code:
```int round(float x)
{
int y = (int)x;
if((x - y) > 0.5f)
{
++y;
}
return y;
}```

3. Test with some negative values too.

4. Originally Posted by major_small
why are you casting y to a float before assigning it to x? and why are you returning x?

why not just increment or decrement y and return it?
heh, I don't know. I whipped it up pretty quickly. Thanks for the input.

5. it's work fine

6. No it doesn't. Read Dave's post.

Quzah.

7. Well I could test for a negative value and handle it accordingly but my program wont ever use the function with a negative value. If I intended to reuse this function then I would but in this situation it is useless to my program.

8. Code:
`#include <math.h>`
Code:
```int round(float x) {
return (int) floor(x + 0.5);
}```
This should round -1.5 to -1, which is okay depending on what your definition of "up" is. (-1.51 rounds to -2)

Code:
```int round(float x) {
return (int) (x - 0.5 + (x > 0.0));
}```
This should round -1.5 to -2, and 1.5 to 2, which is good if you have this definition of "up".