So, according to Apple's documentation about NSRoundBankers:
Round to the closest possible return value; when halfway between two possibilities, return the possibility whose last digit is even.
While this is true for positive numbers, I am not getting the expected behaviour on negative numbers. Here is the piece of code I executed on the device and on the simulator, both printing the exact same results:
NSDecimalNumber *increment = [NSDecimalNumber decimalNumberWithMantissa:5 exponent:-2 isNegative:NO];
NSDecimalNumber *number = [NSDecimalNumber decimalNumberWithMantissa:10 exponent:-1 isNegative:YES];
NSDecimalNumberHandler *handler = [NSDecimalNumberHandler decimalNumberHandlerWithRoundingMode:NSRoundBankers scale:1 raiseOnExactness:NO raiseOnOverflow:NO raiseOnUnderflow:NO raiseOnDivideByZero:YES];
while ([number compare:@1] == NSOrderedAscending)
{
NSLog(@";%@;%@", number, [number decimalNumberByRoundingAccordingToBehavior:handler]);
number = [number decimalNumberByAdding:increment];
}
On negative numbers, it's not returning the one whose last digit is even, it basically rounds down.
For example, for -0.85 i should be getting -0.8, but I am getting -0.9
Am I doing something wrong?
Left table shows the ACTUAL behaviour, in red marked the wrong rounded values.
Right table shows the EXPECTED behaviour, in green the correct rounded values.