Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Round up or down

I have a float which I'd like to round up or down to the nearest integer.

For example:

1.4 = 1.0 1.77 = 2.0 1.1 = 1.0

etc...

I'm doing this in Objective C, so I guess I need a standard math function... Would nearbyintf do it?

like image 741
mootymoots Avatar asked Feb 28 '26 05:02

mootymoots


2 Answers

You can use any of the standard C library math functions defined in math.h. nearbyintf would work, as would roundf or ceilf or floorf, depending on what you want.

Xcode will show you the documentation (UNIX manual page) for these functions if you option-double-click them in the editor.

like image 134
benzado Avatar answered Mar 01 '26 17:03

benzado


use

double a = 2.3;
double b = round(a);

You can find round and other math functions in math.h

like image 33
mjisrawi Avatar answered Mar 01 '26 17:03

mjisrawi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!