NSDecimalNumber *no1    = [NSDecimalNumber decimalNumberWithString:[totalPotFund stringByReplacingOccurrencesOfString:@"," withString:@"."]];//consider totalPotFund = 301
NSLog(@"Total Bank Fund: %@", totalPotFund);

NSDecimalNumber *no2    = [NSDecimalNumber decimalNumberWithString:[MinimumFund stringByReplacingOccurrencesOfString:@"," withString:@"."]];//consider MinimumFund = 1000
NSLog(@"Debet: %@", MinimumFund);

NSDecimalNumber *result = [no1 decimalNumberBySubtracting:no2];//301 - 1000 = -699
NSLog(@"Result = Total Bank Fund - Debet: %@ - %@ = %@", totalPotFund, MinimumFund, result);

NSDecimalNumber *no3  = [NSDecimalNumber decimalNumberWithString:[payAmt.text stringByReplacingOccurrencesOfString:@"," withString:@"."]];//consider payAmt = 12
NSLog(@"Amount to be paid: %@", payAmt.text);

NSComparisonResult res = [result compare:no3]; //-699 compare 12: -699 < 12

NSLog(@"Compare Resultant Amount with CheckOut Amount: %@ compare %@", result, no3);//

if(res == NSOrderedAscending)
{
    Do Something
}
else
{
    Do The Other Thing
}

大家好,我面临的问题是-699小于12,但仍然会
“if(res == NSOrderedAscending)”条件,而不是“else”条件。有趣的是它应该这样做。我还没有这样的示例代码,可以将2个NSDecimal数字与NSComparisonResult比较,并将比较后的结果发送到NSOrderedAscending或NSOrderedDescending或NSOrderedSame。

谢谢您的期待。

最佳答案

从苹果文档:

如果十进制数字的值大于的NSOrderedAscending
接收器; NSOrderedSame如果相等则相同;和NSOrderedDescending如果
decimalNumber的值小于接收者。
NSOrderedAscending是正确的结果,因为参数(no3 = 12)大于接收者(结果= -699)。

08-05 23:50