PruitIgoe
PruitIgoe

Reputation: 6384

iOS: NSDecimalNumber: not getting expected results

Say I have the following values for these ivars:

NSString* percentage = 35 NSString* value = 146

In my code below I am expecting to get 51.1 when I do my multiplication (146*.35) but instead am getting 100.

- (NSDecimalNumber*) getProductByPercentage : (NSArray*) itemsToMultiply : (float) annualPatients {

    //get items from array
    NSString* percentage = [itemsToMultiply objectAtIndex:0];
    NSString* value = [itemsToMultiply objectAtIndex:1];

    //init a number formatter
    NSNumberFormatter * f = [[NSNumberFormatter alloc] init];
    //set its formatting style
    [f setNumberStyle:NSNumberFormatterDecimalStyle];

    //convert to percentage
    float thisPercentage = [percentage floatValue]/100;

    //convert to NSDecimal
    NSDecimalNumber* decPercentage = [[NSDecimalNumber alloc] initWithString:[NSString stringWithFormat:@"%f",  thisPercentage]];
    NSDecimalNumber* decAvgReimbursement = [[NSDecimalNumber alloc] initWithString: value];
    NSDecimalNumber* decAnnualPatients = [[NSDecimalNumber alloc] initWithString:[NSString stringWithFormat:@"%f", annualPatients]];

    //set up handler
    NSDecimalNumberHandler *handler = [NSDecimalNumberHandler decimalNumberHandlerWithRoundingMode:NSRoundPlain
            scale:-2
            raiseOnExactness:NO
            raiseOnOverflow:NO
            raiseOnUnderflow:NO
            raiseOnDivideByZero:NO
    ];

    //get our results
    NSDecimalNumber* decAvgCost = [decAvgReimbursement decimalNumberByMultiplyingBy:decPercentage
                                                                 withBehavior:handler];

    NSLog(@"%@::%@::%@", decAvgCost, decAvgReimbursement, decPercentage);

    return decAvgCost;
}

Here's my log output

2013-02-04 13:57:46.960 EUS Calculator[34154:c07] 100::146::0.35

Is there something wrong with my handler that is getting me the wrong results?

Upvotes: 0

Views: 387

Answers (3)

Joachim Isaksson
Joachim Isaksson

Reputation: 181077

scale:-2

You're setting the scale to round to -2 decimals, that is to the closest 100.

Upvotes: 2

Ismael
Ismael

Reputation: 3937

Change this:

float thisPercentage = [percentage floatValue]/100;

to this:

float thisPercentage = [percentage floatValue]/100.0;

If you don't put the .0, then the compiler interprets it as an integer number, and when doing float/int it will convert it to int, transforming your expected 0.35 to 0.

Also, you aren't using your NSNumberFormatter anywhere, so you might as well not even create it.

Why are you using NSDecimalNumbers anyways?

Upvotes: 1

Nabou
Nabou

Reputation: 549

You're rounding to the wrong scale. Try changing the scale to 0 (removes decimal point) and try again. If you want 1 decimal place, use a scale of 1; you should get 51.1 then.

Upvotes: 1

Related Questions