Rounding double numbers in Objective-C

I need C code to round a double value to the next largest integer value.

For example, if I have:

1.0 (double) it should be 1 (int)
1.01 (double) it should be 2 (int)
5.67 (double) it should be 6 (int)
76.43 (double) it should be 77 (int)

Is there a solution?

+8
c double objective-c int rounding
source share
2 answers

Use ceil() from <math.h> :

 #include <math.h> double x = 1.01; // x = 1.01 double y = ceil(x); // y = 2.0 int i = (int)y; // i = 2 

or more briefly if you just want to get the result of int:

 int i = (int)ceil(x); 
+30
source share

Let's say your floating point number is nr . No built-in functions:

 float nr; int temp; if (nr % 10 > 0) { temp = nr++; } else { temp = nr; } nr = temp; 
-one
source share

All Articles