开发者

How to convert a float to an Int by rounding to the nearest whole integer

开发者 https://www.devze.com 2022-12-16 02:27 出处:网络
Is there a way to convert a float to an Int by rounding to the nearest possible wh开发者_JAVA百科ole integer? To round to the nearest use roundf(), to round up use ceilf(), to round down use floorf().

Is there a way to convert a float to an Int by rounding to the nearest possible wh开发者_JAVA百科ole integer?


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


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.


How about this:

float f = 1.51;
int i = (int) (f + 0.5);


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.


(int)floor(f+0.5);

Try this...

0

精彩评论

暂无评论...
验证码 换一张
取 消