假设我对这些 ivars 有以下值:
NSString* 百分比 = 35 NSString* 值 = 146
在下面的代码中,当我进行乘法 (146*.35) 时,我期望得到 51.1,但得到的是 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;
}
这是我的日志输出
2013-02-04 13:57:46.960 EUS Calculator[34154:c07] 100::146::0.35
我的处理程序有什么问题导致我得到错误的结果吗?