I am using NSDecimal because I have to store extremely large values for my application. I would like to be able to use the NSDecimalDivide function to divide two NSDecimals and round the result to the nearest integer.
NSDecimal testOne = [[NSDecimalNumber decimalNumberWithString:@"1064"] decimalValue];
NSDecimal cost = [[NSDecimalNumber decimalNumberWithString:@"10"]decimalValue];
NSDecimal value;
NSDecimalDivide(&value, &testOne, &cost, NSRoundDown);
NSString *string = NSDecimalString(&value, _usLocale);
NSLog(@"Here is the cost%@",string);
This out puts 106.4 I would like it to output 106. Does anyone know how to round a NSDecimal number to the integer value?
One possible way to do that is using a
NSDecimalNumberHandler
withscale
set to0
.Note that I am not using the primitive
NSDecimal
at all and you I would advise you the same.However, if you want to work with
NSDecimal
directly, you can use the handyNSDecimalRound
function.