Skip to content

Instantly share code, notes, and snippets.

@Morse-Code
Last active August 29, 2015 13:59
Show Gist options
  • Save Morse-Code/10999079 to your computer and use it in GitHub Desktop.
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).
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);
@evanlaird
Copy link

"Not too verbose"... maybe compared to other objc :p

@Morse-Code
Copy link
Author

its awful

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment