gpt4 book ai didi

两个 NSDecimalNumber 对象的 iphone NSComparisonResult 失败

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:09:22 25 4
gpt4 key购买 nike

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 if the value of decimalNumber is greater than the receiver; NSOrderedSame if they’re equal; and NSOrderedDescending if the value of decimalNumber is less than the receiver.

NSOrderedAscending 是正确的结果,因为参数 (no3 = 12) 大于接收者 (result = -699)。

关于两个 NSDecimalNumber 对象的 iphone NSComparisonResult 失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7025223/

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