25

Is there a way to convert a float to an Int by rounding to the nearest possible whole integer?

1

5 Answers 5

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

https://developer.apple.com/library/archive/documentation/System/Conceptual/ManPages_iPhoneOS/man3/roundf.3.html

https://developer.apple.com/library/archive/documentation/System/Conceptual/ManPages_iPhoneOS/man3/ceil.3.html

https://developer.apple.com/library/archive/documentation/System/Conceptual/ManPages_iPhoneOS/man3/floor.3.html

1
  • 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
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.

2
  • 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. Commented Jan 10, 2010 at 5:28
  • round() has non-standard rounding semantics: halfway cases round away from zero. The best choice is usually nearbyint() (or nearbyintf/l), 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. Commented Jun 3, 2016 at 21:57
3

How about this:

float f = 1.51;
int i = (int) (f + 0.5);
4
  • it works. But, how? shouldnt the int be 2.01? then how does the int go down?
    – Sam Jarman
    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
  • well if thats true, then shouldn't i = f ; just work?
    – Sam Jarman
    Commented Jan 10, 2010 at 4:17
  • 6
    Doesn't work for negative numbers, see ergosys for correct answer. Commented Jan 10, 2010 at 4: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

Working example for C++ here.

-1
(int)floor(f+0.5);

Try this...

Not the answer you're looking for? Browse other questions tagged or ask your own question.