Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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

like image 892
Sam Jarman Avatar asked Jan 10 '10 03:01

Sam Jarman


People also ask

How do you convert floating to int and rounding?

round() method, which converts float to its nearest integer by adding +0.5 to it's value and then truncating it.

How do you round a float to the nearest whole number?

Round() Round() is a built-in function available with python. It will return you a float number that will be rounded to the decimal places which are given as input. If the decimal places to be rounded are not specified, it is considered as 0, and it will round to the nearest integer.

Does float to int round up or down?

Reference (thanks Rawling) Explicit Numeric Conversions Table: When you convert a double or float value to an integral type, this value is rounded towards zero to the nearest integral value.


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

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

like image 103
Oliver Pearmain Avatar answered Sep 22 '22 03:09

Oliver Pearmain