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?

有帮助吗?

解决方案

scale:-2

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

其他提示

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.

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?

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top