简体   繁体   中英

How to round off a double value to the next integer value in Objective-C?

I know it's quite a silly question, but I really am finding the solution for the same. Suppose I am having a variable, db1 , with a value 4.166667, and I want to convert it to an integer with the value 5. How should I do that?

#include <math.h>

int db1_int = (int)ceil(db1);

You can round it using the ceil function found in math.h.

double notRounded = 4.1666667
int rounded = (int)ceil(notRounded);

Don't forget to #include <math.h>

See Stack Overflow question Is there a function to round a float in C or do I need to write my own? .

Objective-C works on top of C, so you can use this code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM