How do I round a NSNumber to zero decimal spaces
Asked Answered
K

5

6

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];
Kisner answered 2/8, 2010 at 13:12 Comment(1)
duplicate #576610Boardwalk
C
3

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];
Chiliarch answered 2/8, 2010 at 13:28 Comment(3)
floor is more likely the desired behaviorCodeclination
@JoshuaWeinberg Technically this should truncate, which is the same as floor.Xiphoid
Floor and truncation are different for negatives.Codeclination
H
15

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

int myInt = (int)(sHolidayDuration.value + 0.5);
Handoff answered 2/8, 2010 at 13:38 Comment(3)
That is such a witty way of getting around the round up. I had never thought of doing it that way cause of built in rounding tools in ios. I like this answer a lot way to be thinking.Kiarakibble
This wouldn't really work with negative numbers thoughKuvasz
For negative numbers try this int myInt = (int)(sHolidayDuration.value > 0 ? sHolidayDuration.value + 0.5 : sHolidayDuration.value - 0.5);Mode
N
12

Here's a bit of a long winded approach

float test = 1.9;
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
[formatter setRoundingMode:NSNumberFormatterRoundHalfUp];
[formatter setMaximumFractionDigits:0];
NSLog(@"%@",[formatter  stringFromNumber:[NSNumber numberWithFloat:test]]);
[formatter release];
Nape answered 13/9, 2011 at 14:37 Comment(1)
This is more what I was looking for. In my case I wanted a decimal place or two, and in that respect this method is more powerful.Xiphoid
C
3

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];
Chiliarch answered 2/8, 2010 at 13:28 Comment(3)
floor is more likely the desired behaviorCodeclination
@JoshuaWeinberg Technically this should truncate, which is the same as floor.Xiphoid
Floor and truncation are different for negatives.Codeclination
D
1

you can also do the following: int roundedRating = (int)round(rating.floatValue);

Dodder answered 19/7, 2013 at 20:13 Comment(0)
G
0

Floor the number using the old C function floor() and then you can create an NSInteger which is more appropriate, see: https://developer.apple.com/library/mac/documentation/Darwin/Reference/ManPages/man3/floor.3.html ....

NSInteger holidayNightCount = [NSNumber numberWithInteger:floor(sHolidayDuration.value)].integerValue;

Further information on the topic here: http://eureka.ykyuen.info/2010/07/19/objective-c-rounding-float-numbers/

Or you could use NSDecimalNumber features for rounding numbers.

Gualterio answered 28/8, 2014 at 12:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.