Last active
August 29, 2015 13:59
-
-
Save Morse-Code/10999079 to your computer and use it in GitHub Desktop.
Use NSDecimalNumberHandler with NSDecimalNumber to round floating point value to closest value of specified precision (scale).
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
float toRound = 0.999322; | |
short precision = 3; | |
NSDecimalNumberHandler *handler = [NSDecimalNumberHandler decimalNumberHandlerWithRoundingMode:NSRoundPlain | |
scale:precision | |
raiseOnExactness:YES | |
raiseOnOverflow:YES | |
raiseOnUnderflow:YES | |
raiseOnDivideByZero:YES]; | |
float rounded = [[[NSDecimalNumber decimalNumberWithString:[[NSNumber numberWithFloat:toRound] stringValue]] decimalNumberByRoundingAccordingToBehavior:handler] floatValue]; | |
NSLog(@"%f", rounded); |
"Not too verbose"... maybe compared to other objc :p
its awful
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Pretty sweet. Not too verbose either.