cyclingIsBetter
cyclingIsBetter

Reputation: 17591

Rounding up double numbers in Objective-C

I need C code for rounding up a double value to the next greatest integer value.

For example, if I have:

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

Is there a solution?

Upvotes: 8

Views: 9087

Answers (2)

Paul R
Paul R

Reputation: 212969

Use the ceil() function 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 concisely, if you just want the int result:

int i = (int)ceil(x);

Upvotes: 30

MMMM
MMMM

Reputation: 1317

Let's say your float number is nr. No build-in functions:

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

Upvotes: -1

Related Questions