Objective c 添加返回nil的NSDecimalNumber
我有这个密码Objective c 添加返回nil的NSDecimalNumber,objective-c,nsdecimalnumber,Objective C,Nsdecimalnumber,我有这个密码 int countCosts = [Costs count]; countCosts = countCosts - 1; NSDecimalNumber* Total = [[NSDecimalNumber alloc] initWithString:[NSString stringWithFormat:@"%f", 0.0]]; NSDecimalNumber *cost = 0; while (countCosts != -1) { cos
int countCosts = [Costs count];
countCosts = countCosts - 1;
NSDecimalNumber* Total = [[NSDecimalNumber alloc] initWithString:[NSString stringWithFormat:@"%f", 0.0]];
NSDecimalNumber *cost = 0;
while (countCosts != -1)
{
cost = [Costs objectAtIndex:countCosts];
Total = [Total decimalNumberByAdding:cost];
countCosts = countCosts - 1;
if (countCosts < 0)
break;
}
int countCosts=[Costs count];
countCosts=countCosts-1;
NSDecimalNumber*Total=[[NSDecimalNumber alloc]initWithString:[NSString stringWithFormat:@“%f”,0.0]];
NSDecimalNumber*成本=0;
while(countCosts!=-1)
{
成本=[成本对象索引:countCosts];
总计=[总小数位数加总:成本];
countCosts=countCosts-1;
如果(成本<0)
打破
}
成本(数组中有1.10、2.25、3.50个)。
总计应等于成本中所有项目的总计。但等于0 在
成本
数组中添加所有成本的逻辑有点奇怪。试试这个:
NSDecimalNumber *total = [NSDecimalNumber zero];
for (NSString *cost in Costs) {
NSDecimalNumber *num = [NSDecimalNumber decimalNumberWithString:cost];
total = [total decimalNumberByAdding:num];
}
NSLog(@"total = %@", total);
顺便说一句-标准命名约定规定类以大写字母开头,而方法和变量以小写字母开头。在
costs
数组中添加所有开销的逻辑有点奇怪。试试这个:
NSDecimalNumber *total = [NSDecimalNumber zero];
for (NSString *cost in Costs) {
NSDecimalNumber *num = [NSDecimalNumber decimalNumberWithString:cost];
total = [total decimalNumberByAdding:num];
}
NSLog(@"total = %@", total);
顺便说一句-标准命名约定规定类以大写字母开头,而方法和变量以小写字母开头。在
costs
数组中添加所有开销的逻辑有点奇怪。试试这个:
NSDecimalNumber *total = [NSDecimalNumber zero];
for (NSString *cost in Costs) {
NSDecimalNumber *num = [NSDecimalNumber decimalNumberWithString:cost];
total = [total decimalNumberByAdding:num];
}
NSLog(@"total = %@", total);
顺便说一句-标准命名约定规定类以大写字母开头,而方法和变量以小写字母开头。在
costs
数组中添加所有开销的逻辑有点奇怪。试试这个:
NSDecimalNumber *total = [NSDecimalNumber zero];
for (NSString *cost in Costs) {
NSDecimalNumber *num = [NSDecimalNumber decimalNumberWithString:cost];
total = [total decimalNumberByAdding:num];
}
NSLog(@"total = %@", total);
顺便说一句-标准命名约定规定类以大写字母开头,而方法和变量以小写字母开头。在循环开始之前,什么是
Total
?Costs
数组是否包含NSDecimalNumber
s?总计为0,Costs数组包含plist上的内容。2.60 1.10 1.10 1.10 1.10 2.85 2.85 1.10 1.10 1.10 1.10 1.10 2.85 2.85 1.10 1.10 1.10这将值显示为字符串,而不是数字。在何处将每个字符串转换为NSDecimalNumber
?循环开始前的总数是多少?Costs
数组是否包含NSDecimalNumber
s?总计为0,Costs数组包含plist上的内容。2.60 1.10 1.10 1.10 1.10 2.85 2.85 1.10 1.10 1.10 1.10 1.10 2.85 2.85 1.10 1.10 1.10这将值显示为字符串,而不是数字。在何处将每个字符串转换为NSDecimalNumber
?循环开始前的总数是多少?Costs
数组是否包含NSDecimalNumber
s?总计为0,Costs数组包含plist上的内容。2.60 1.10 1.10 1.10 1.10 2.85 2.85 1.10 1.10 1.10 1.10 1.10 2.85 2.85 1.10 1.10 1.10这将值显示为字符串,而不是数字。在何处将每个字符串转换为NSDecimalNumber
?循环开始前的总数是多少?Costs
数组是否包含NSDecimalNumber
s?总计为0,Costs数组包含plist上的内容。2.60 1.10 1.10 1.10 1.10 2.85 2.85 1.10 1.10 1.10 1.10 1.10 2.85 2.85 1.10 1.10 1.10这将值显示为字符串,而不是数字。您在哪里将每个字符串转换为一个NSDecimalNumber
?代码工作(某种程度上)总数等于0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000201954995630462021096371054384448,而不是它应该的26.10。有什么想法吗?在调试器中运行代码,并在循环的每个迭代中查看total
和cost
。您是否看到任何意外情况?成本是正常的,但总成本会越来越小。您是否确定成本
数组中的每个值都是NSDecimalNumber
?成本数组包含plist上的内容。2.60 1.10 1.10 1.10 1.10 2.85 2.10 1.10 1.10 1.10 1.10 2.85 2.85 1.10 1.10 1.10 1.10代码工作(排序)总结果等于0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002019549956304021096371054384448,而不是它应该达到的26.10。有什么想法吗?在调试器中运行代码,并在循环的每个迭代中查看total
和cost
。您是否看到任何意外情况?成本是正常的,但总成本会越来越小。您是否确定成本
数组中的每个值都是NSDecimalNumber
?成本数组包含plist上的内容。2.60 1.10 1.10 1.10 1.10 2.85 2.10 1.10 1.10 1.10 1.10 2.85 2.85 1.10 1.10 1.10 1.10代码工作(排序)总结果等于0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002019549956304021096371054384448,而不是它应该达到的26.10。有什么想法吗?在调试器中运行代码,并在循环的每个迭代中查看total
和cost
。您是否看到任何意外情况?成本是正常的,但总成本会越来越小。您是否确定成本
数组中的每个值都是NSDecimalNumber
?成本数组包含plist上的内容。2.60 1.10 1.10 1.10 1.10 2.85 2.10 1.10 1.10 1.10 1.10 2.85 2.85 1.10 1.10 1.10 1.10代码工作(排序)总结果等于0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002019549956304021096371054384448,而不是它应该达到的26.10。有什么想法吗?在调试器中运行代码,并在循环的每个迭代中查看total
和cost
。您是否看到任何意外情况?成本是正常的,但总成本会越来越小。您是否确定成本
数组中的每个值都是NSDecimalNumber
?成本数组包含plist上的内容。2.60 1.10 1.10 1.10 1.10 2.85 2.85 1.10 1.10 1.10 1.10 1.10 2.85 2.85 1.10 1.10