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];
Use lroundf() to round a float to integer and then convert the integer to a string.
Rounding Numbers in Swift By using round(_:) , ceil(_:) , and floor(_:) you can round Double and Float values to any number of decimal places in Swift.
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);
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];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With