NSRoundBankers not working as described by Apple

So, according to Apple's documentation on NSRoundBankers :

rounding to the maximum possible return value; when halfway between two possibilities, we return the opportunity whose last digit is even.

As long as this is true for positive numbers, I don't get the expected behavior on negative numbers . Here is a piece of code that I have executed on the device and on the simulator, both print exact 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 doesn't return the one with the last digit even, it basically rounds up.

For example, for -0.85 I should be getting -0.8 , but I am getting -0.9

Am I doing something wrong?

The left table shows the behavior of ACTUAL, incorrect rounded values ​​are marked in red.

The right table shows the behavior of EXPECTED, in green the correct rounded values.

iOS Java

+3


source to share





All Articles