Я создаю приложение, которое занимается подсчетом денег и прочитал, что для этого всегда лучше использовать NSNumber. Поэтому я изменил мои значения с плавающей запятой на NSNumber, но теперь я получаю следующий странный результат, и я не знаю, почему это происходит.
Это код:
- (NSDecimalNumber*) subtotal {
NSDecimalNumber *subtotal = [NSDecimalNumber decimalNumberWithString:@"0"];
NSLog(@"subtotal initially set at %@",subtotal);
for (NSManagedObject *object in [fetchedResultsController fetchedObjects]) {
NSLog( @"Looping");
NSDecimalNumber *objectRowTotalNumber = [object valueForKey:@"total"];
NSLog(@"object row total number = %@",objectRowTotalNumber);
subtotal = [subtotal decimalNumberByAdding:objectRowTotalNumber];
NSLog(@"Subtotal: %@", subtotal);
}
return 0;
}
Это вывод в журнале:
**2011-10-19 09:27:19.289 Market[4240:b603] subtotal initially set at 0**
**2011-10-19 09:27:19.290 Market[4240:b603] Looping**
**2011-10-19 09:27:19.291 Market[4240:b603] object row total number = 5**
**2011-10-19 09:27:19.292 Market[4240:b603] Subtotal: -0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000244200806570181766106054656**
Кто-нибудь знает, что вызывает странный результат, когда промежуточный итог должен быть просто установлен на 5?
Последний тест : NSDecimalNumberMethod работает только с новыми объектами, которые я добавляю в сеанс, но не с объектами, которые загружаются
-(void) viewDidAppear:(BOOL)animated {
NSLog(@"I'm in ViewDidAppear");
// Reload subtotal via float method
[self subtotal];
//Reload subtotal via decimal method
[self subtotalDec];
}
Расчет методом поплавка
-(float) subtotal {
NSLog(@"I'm in subtotal using floats");
float subtotal = 0;
NSLog(@"subtotal initially set at %@",subtotal);
for (NSManagedObject *object in [fetchedResultsController fetchedObjects]) {
NSLog( @"Looping");
NSNumber *objectRowTotalNumber = [object valueForKey:@"total"];
NSLog(@"object row total number = %@",objectRowTotalNumber);
float objectRowTotal = [objectRowTotalNumber floatValue];
subtotal = subtotal + objectRowTotal;
}
NSLog(@"Subtotal: %f", subtotal);
return 0;
}
Расчет по методу NSDecimal Number
- (NSDecimalNumber*) subtotalDec {
NSLog(@"I'm in subtotal using NSDecimalNumber");
NSDecimalNumber *subtotal = [NSDecimalNumber decimalNumberWithString:@"0"];
NSLog(@"subtotal initially set at %@",subtotal);
for (NSManagedObject *object in [fetchedResultsController fetchedObjects]) {
NSLog( @"Looping");
NSDecimalNumber *objectRowTotalNumber = [object valueForKey:@"total"];
NSLog(@"object row total number = %@",objectRowTotalNumber);
subtotal = [subtotal decimalNumberByAdding:objectRowTotalNumber];
}
NSLog(@"Subtotal: %@",subtotal);
return 0;
}
журнал для просмотра Появится
2011-10-19 11:19:01.506 Market[961:b603] I'm in ViewDidAppear
2011-10-19 11:19:01.506 Market[961:b603] I'm in subtotal using floats
2011-10-19 11:19:01.507 Market[961:b603] subtotal initially set at (null)
2011-10-19 11:19:01.508 Market[961:b603] Looping
2011-10-19 11:19:01.508 Market[961:b603] object row total number = 4
2011-10-19 11:19:01.509 Market[961:b603] Looping
2011-10-19 11:19:01.509 Market[961:b603] object row total number = 1
2011-10-19 11:19:01.509 Market[961:b603] Looping
2011-10-19 11:19:01.510 Market[961:b603] object row total number = 10
2011-10-19 11:19:01.510 Market[961:b603] Subtotal: 15.000000
2011-10-19 11:19:01.510 Market[961:b603] I'm in subtotal using NSDecimalNumber
2011-10-19 11:19:01.511 Market[961:b603] subtotal initially set at 0
2011-10-19 11:19:01.511 Market[961:b603] Looping
2011-10-19 11:19:01.511 Market[961:b603] object row total number = 4
2011-10-19 11:19:01.512 Market[961:b603] Subtotal: -0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000244200806569055866199212032
2011-10-19 11:19:01.512 Market[961:b603] Looping
2011-10-19 11:19:01.512 Market[961:b603] object row total number = 1
2011-10-19 11:19:01.513 Market[961:b603] Subtotal: -0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000488401613129104533143683072
2011-10-19 11:19:01.513 Market[961:b603] Looping
2011-10-19 11:19:01.513 Market[961:b603] object row total number = 10
2011-10-19 11:19:01.514 Market[961:b603] Subtotal: -0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000732602419703789898877108224
журнал для addbuttontapped: обратите внимание, как NSDecimalNumberMethod работает только с последним добавленным значением, а не с теми, которые были введены до запуска программы
2011-10-20 07:17:03.218 Market[2199:b603] I'm in subtotal using floats
2011-10-20 07:17:03.218 Market[2199:b603] subtotal initially set at (null)
2011-10-20 07:17:03.219 Market[2199:b603] Looping
2011-10-20 07:17:03.219 Market[2199:b603] object row total number = 4
2011-10-20 07:17:03.219 Market[2199:b603] Looping
2011-10-20 07:17:03.219 Market[2199:b603] object row total number = 1
2011-10-20 07:17:03.220 Market[2199:b603] Looping
2011-10-20 07:17:03.220 Market[2199:b603] object row total number = 10
2011-10-20 07:17:03.220 Market[2199:b603] Looping
2011-10-20 07:17:03.221 Market[2199:b603] object row total number = 2
2011-10-20 07:17:03.221 Market[2199:b603] Subtotal: 17.000000
2011-10-20 07:17:03.221 Market[2199:b603] I'm in subtotal using NSDecimalNumber
2011-10-20 07:17:03.222 Market[2199:b603] subtotal initially set at 0
2011-10-20 07:17:03.222 Market[2199:b603] Looping
2011-10-20 07:17:03.222 Market[2199:b603] object row total number = 4
2011-10-20 07:17:03.223 Market[2199:b603] Subtotal: -0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000244200806569055866199212032
2011-10-20 07:17:03.223 Market[2199:b603] Looping
2011-10-20 07:17:03.224 Market[2199:b603] object row total number = 1
2011-10-20 07:17:03.224 Market[2199:b603] Subtotal: -0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000488401613129104533143683072
2011-10-20 07:17:03.224 Market[2199:b603] Looping
2011-10-20 07:17:03.225 Market[2199:b603] object row total number = 10
2011-10-20 07:17:03.225 Market[2199:b603] Subtotal: -0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000732602419703789898877108224
2011-10-20 07:17:03.225 Market[2199:b603] Looping
2011-10-20 07:17:03.226 Market[2199:b603] object row total number = 2
2011-10-20 07:17:03.226 Market[2199:b603] Subtotal: 2