Is there a way to convert a float
to an Int
by rounding to the nearest possible whole integer?
Asked
Active
Viewed 5.3k times
25

Vadim
- 8,701
- 4
- 43
- 50

Sam Jarman
- 7,277
- 15
- 55
- 100
-
See also [a more recent `[c++]` Q&A](http://stackoverflow.com/questions/37620659/merit-of-inline-asm-rounding-via-putting-float-into-int-variable/37624488#37624488) for what actually compiles to fast code (`lrintf(x)` or `(int)nearbyintf(x)`) – Peter Cordes Jun 04 '16 at 02:34
5 Answers
57
To round to the nearest use roundf(), to round up use ceilf(), to round down use floorf(). Hopefully this example demonstrates...
#import "math.h"
...
float numberToRound;
int result;
numberToRound = 4.51;
result = (int)roundf(numberToRound);
NSLog(@"roundf(%f) = %d", numberToRound, result); // roundf(4.510000) = 5
result = (int)ceilf(numberToRound);
NSLog(@"ceilf(%f) = %d", numberToRound, result); // ceilf(4.510000) = 5
result = (int)floorf(numberToRound);
NSLog(@"floorf(%f) = %d", numberToRound, result); // floorf(4.510000) = 4
numberToRound = 10.49;
result = (int)roundf(numberToRound);
NSLog(@"roundf(%f) = %d", numberToRound, result); // roundf(10.490000) = 10
result = (int)ceilf(numberToRound);
NSLog(@"ceilf(%f) = %d", numberToRound, result); // ceilf(10.490000) = 11
result = (int)floorf(numberToRound);
NSLog(@"floorf(%f) = %d", numberToRound, result); // floorf(10.490000) = 10
numberToRound = -2.49;
result = (int)roundf(numberToRound);
NSLog(@"roundf(%f) = %d", numberToRound, result); // roundf(-2.490000) = -2
result = (int)ceilf(numberToRound);
NSLog(@"ceilf(%f) = %d", numberToRound, result); // ceilf(-2.490000) = -2
result = (int)floorf(numberToRound);
NSLog(@"floorf(%f) = %d", numberToRound, result); // floorf(-2.490000) = -3
numberToRound = -3.51;
result = (int)roundf(numberToRound);
NSLog(@"roundf(%f) = %d", numberToRound, result); // roundf(-3.510000) = -4
result = (int)ceilf(numberToRound);
NSLog(@"ceilf(%f) = %d", numberToRound, result); // ceilf(-3.510000) = -3
result = (int)floorf(numberToRound);
NSLog(@"floorf(%f) = %d", numberToRound, result); // floorf(-3.510000) = -4
The documentation...

Denis Murphy
- 1,137
- 1
- 11
- 21

Oliver Pearmain
- 19,885
- 13
- 86
- 90
-
1`(int)nearbyintf(x)` compiles to better asm on x86, because it uses the current rounding mode instead of a fixed rounding mode (with a quirk that x86 doesn't support in hardware). – Peter Cordes Jun 04 '16 at 02:37
27
Actually Paul Beckingham's answer isn't quite correct. If you try a negative number like -1.51, you get -1 instead of -2.
The functions round(), roundf(), lround(), and lroundf() from math.h work for negative numbers too.

ergosys
- 47,835
- 5
- 49
- 70
-
Rounding is one of those things you can get into arguments with others for hours. There's also a school of thought that says the "add a half and drop the fractional part" biases the outputs upward (because `k + 0` doesn't change and `k + 0.5` always goes up for a given integer `k`), so they recommend rounding even halves down and odd halves up (i.e. `k + 0.5` rounds to `k` for `k` = ..., -4, -2, 0, 2, 4, ... and it rounds to `k + 1` for `k` = ..., -3, -1, 1, 3, ...). Basically, the naive way can throw statistics off. – Mike DeSimone Jan 10 '10 at 05:28
-
`round()` has non-standard rounding semantics: halfway cases round away from zero. [**The best choice is usually `nearbyint()` (or `nearbyintf`/`l`)**](http://en.cppreference.com/w/c/numeric/math/nearbyint), because it can be done with a single machine instruction on x86 CPUs with SSE4.1. (Or with SSE1 for converting to an `int` or `long` at the same time). (`rint` is similar, but it's required to raise the FP "inexact" exception when the result isn't the same as the input.) [Even with `-ffast-math`, `round()` doesn't inline.](https://godbolt.org/g/51eXsy) – Peter Cordes Jun 03 '16 at 21:57
3
How about this:
float f = 1.51; int i = (int) (f + 0.5);

Paul Beckingham
- 14,495
- 5
- 33
- 67
-
it works. But, how? shouldnt the int be 2.01? then how does the int go down? – Sam Jarman Jan 10 '10 at 03:47
-
An integer is by definition a whole number, so how can it possibly have any fractional value? – Quick Joe Smith Jan 10 '10 at 04:12
-
-
6Doesn't work for negative numbers, see ergosys for correct answer. – BlueRaja - Danny Pflughoeft Jan 10 '10 at 04:19
1
round() can round a float to nearest int, but it's output is still a float... so cast round()'s output to an integer:
float input = 3.456;
int result;
result = (int)round(input);
//result is: 3

Vince K
- 305
- 3
- 11