here is my code snip, i dont know how to round double numbers.
double m = [tmpProduct.msrp doubleValue] ;
double d = [tmpProduct.discountPrice doubleValue];
double p = (d * 100 ) / m;
here tmpProduct.msrp and mpProduct.discountPrice are (NSDecimalNUmber *)
from the operation above I got p = 44.995757
i want to convert it to (45%) , how do i do that?
here is what i use , but that does not help.
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
[formatter setMaximumFractionDigits:2];
[formatter setRoundingMode: NSNumberFormatterRoundDown];
[formatter stringFromNumber:[NSNumber numberWithDouble:p]]
Have you tried using the arithmetic operations NSDecimalNumber provides?
I haven’t tried that myself, but it seems you are bypassing the advantages of NSDecimalNumber by using standard operators ( *, /, …) on the double values of your number instances.
Take a look at NSDecimalNumber: Performing Arithmetic
As Objective-C does not support operator overloading you will have to use methods such as:
Update:
Division using NSDecimalNumber:
by using
descriptionWithLocale:, you get the correctNSLocaleDecimalSeparatorUpdate 2:
I forgot about the rounding:
NSDecimalNumber supports several rounding modes.
Read the documentation of
decimalNumberByRoundingAccordingToBehavior: