tags:

views:

45

answers:

2

How do I round an NSNumber to zero decimal spaces, in the following line it seems to keep the decimal spaces:

NSNumber holidayNightCount = [NSNumber numberWithDouble:sHolidayDuration.value];
A: 

If you only need an integer why not just use an int

int holidayNightCount = (int)sHolidayDuration.value;

By definition an int has no decimal places

If you need to use NSNumber, you could just cast the Double to Int and then use the int to create your NSNumber.

int myInt = (int)sHolidayDuration.value;
NSNumber holidayNightCount = [NSNumber numberWithInt:myInt];
esde84
floor is more likely the desired behavior
Joshua Weinberg
+1  A: 

Typically casting to int truncates. For example, 3.4 becomes 3 (as is desired), but 3.9 becomes 3 also. If this happens, add on-half before casting

int myInt = (int)(sHolidayDuration.value + 0.5);
Paul E.