gpt4 book ai didi

iOS:NSDecimalNumber:没有得到预期的结果

转载 作者:行者123 更新时间:2023-11-28 19:12:09 26 4
gpt4 key购买 nike

假设我对这些 ivar 具有以下值:

NSString* 百分比 = 35NSString* 值 = 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

我的处理程序是否有问题导致我得到错误的结果?

最佳答案

scale:-2

您将比例设置为四舍五入到 -2 位小数,即最接近的 100。

关于iOS:NSDecimalNumber:没有得到预期的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14693582/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com