I have a situation which I need to add two NSDecimals and this is the code that I have:
NSDecimalNumber *total = [[NSDecimalNumber alloc] initWithString:@"0"];
for (Product* product in cartItems) {
NSDecimalNumber *prodPrice = [[NSDecimalNumber alloc] init];
prodPrice = product.price;
total = [total decimalNumberByAdding:prodPrice];
}
return total;
It is completely working when I try to add two numbers such as 0.01 and 0.02 and it is giving me the 0.03.
But when I use a whole number it doesn't work. As an example when I try to add 0.01 and 1, it give me a negative number as a result. Can anyone help me with this issue?
Thanks