Is there a way to convert a float
to an Int
by rounding to the nearest possible whole integer?
5 Answers
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...
-
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). Commented Jun 4, 2016 at 2:37
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.
-
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 andk + 0.5
always goes up for a given integerk
), so they recommend rounding even halves down and odd halves up (i.e.k + 0.5
rounds tok
fork
= ..., -4, -2, 0, 2, 4, ... and it rounds tok + 1
fork
= ..., -3, -1, 1, 3, ...). Basically, the naive way can throw statistics off. Commented Jan 10, 2010 at 5:28 -
round()
has non-standard rounding semantics: halfway cases round away from zero. The best choice is usuallynearbyint()
(ornearbyintf
/l
), because it can be done with a single machine instruction on x86 CPUs with SSE4.1. (Or with SSE1 for converting to anint
orlong
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. Commented Jun 3, 2016 at 21:57
How about this:
float f = 1.51; int i = (int) (f + 0.5);
-
it works. But, how? shouldnt the int be 2.01? then how does the int go down? Commented Jan 10, 2010 at 3:47
-
An integer is by definition a whole number, so how can it possibly have any fractional value? Commented Jan 10, 2010 at 4:12
-
-
6Doesn't work for negative numbers, see ergosys for correct answer. Commented Jan 10, 2010 at 4:19
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
[c++]
Q&A for what actually compiles to fast code (lrintf(x)
or(int)nearbyintf(x)
)