不久前我看到过一个类似的话题,但这似乎是因为方程在某处使用了 NSNumber。我检查了我所有的代码,我只能看到我正在为我的方程式使用 NSDecimalNumbers。
我有一个返回一些定价数字的 JSON 网络服务。
在对象属性中解析它们之后,我想用它们运行以下等式:
NetPrice * ( 1 + (RawPercentage / 100)) + FixedPrice = processesNetPrice
更新:我将代码更改为:
NSDecimalNumber *one = [NSDecimalNumber one];
if (!self.netPrice || [self.netPrice isKindOfClass:[NSNull class]])
{ self.netPrice = [NSDecimalNumber zero]; }
NSDecimalNumber *nettoPrijs = self.netPrice;
NSLog(@"self.netPrice = %@",self.netPrice);
//Returns: self.netPrice = 89.25
NSLog(@"nettoPrijs = %@",nettoPrijs);
//Returns: nettoPrijs = 89.25
if (!self.userData.BandFixed || [self.userData.BandFixed isKindOfClass:[NSNull class]])
{ self.userData.BandFixed = [NSDecimalNumber zero]; }
NSDecimalNumber *fixedPrice = self.userData.BandFixed;
NSLog(@"self.userData.BandFixed = %@",self.userData.BandFixed);
//Returns: self.userData.BandFixed = 0
NSLog(@"fixedPrice = %@",fixedPrice);
//Returns: fixedPrice = 0
if (!self.userData.BandPercentage || [self.userData.BandPercentage isKindOfClass:[NSNull class]])
{ self.userData.BandPercentage = [NSDecimalNumber zero]; }
NSDecimalNumber *rawPercentage = self.userData.BandPercentage;
NSLog(@"self.userData.BandPercentage = %@",self.userData.BandPercentage);
//Returns: self.userData.BandPercentage = 0
NSLog(@"rawPercentage = %@",rawPercentage);
//Returns: rawPercentage = 0
NSDecimalNumber *percentage = [rawPercentage decimalNumberByMultiplyingByPowerOf10:-2];
NSLog(@"percentage = %@",percentage);
//Returns: percentage = 0
NSDecimalNumber *onePercent = [one decimalNumberByAdding:percentage];
NSLog(@"onePercent = %@",onePercent);
//Returns: onePercent = 1
NSDecimalNumber *onePercentTimesNetPrice = [onePercent decimalNumberByMultiplyingBy:nettoPrijs];
NSLog(@"onePercentTimesNetPrice = %@",onePercentTimesNetPrice);
//Returns: onePercentTimesNetPrice =
-0.000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000359498933794611654993903616
NSDecimalNumber *addingFixed = [onePercentTimesNetPrice decimalNumberByAdding:fixedPrice];
NSLog(@"addingFixed = %@",addingFixed);
//Returns: addingFixed =
-0.000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000359498933794611654993903616
如果我改变:
NSDecimalNumber *onePercentTimesNetPrice = [onePercent decimalNumberByMultiplyingBy:nettoPrijs];
到:
NSDecimalNumber *onePercentTimesNetPrice = [nettoPrijs decimalNumberByMultiplyingBy:onePercent];
它崩溃了:
-[__NSCFNumber decimalNumberByMultiplyingBy:]: unrecognized selector sent to instance 0x84ad160