I think truncating can be done by converting float to int e.g 25.83f will be 25 when converted to int but it should be 26 when rounded off..how this can be achieved please help me thanks Similarly I think ceiling can be done by converting to int and then adding 1 to it..is it true??
Asked
Active
Viewed 6,221 times
1 Answers
0
Without using round
, floor
, ceil
etc. (without "using build in functions") you can round by simply adding 0.5 and truncating to int:
float x = 25.83f
int y = (int)(x + 0.5f);
But beware of negative numbers, there you might want to do the opposite, i.e. subtract:
float x = -25.83f
int y = (int)(x - 0.5f);
Nevertheless, this is what the build in functions are designed to do, so I don't see many valid reasons to avoid them.

EmDroid
- 5,918
- 18
- 18
-
I think the "valid reason" was to get the OP to do some _thinking_ (antiquated technique predating Stack Overflow) and come up with a solution. Unfortunately they couldn't be bothered to do that. – Lightness Races in Orbit Jun 30 '17 at 14:06