3

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]]
Thomas Zoechling
  • 34,177
  • 3
  • 81
  • 112
Nnp
  • 1,813
  • 7
  • 36
  • 62

2 Answers2

1

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:

- (NSDecimalNumber *)decimalNumberByDividingBy:(NSDecimalNumber *)decimalNumber

Update:
Division using NSDecimalNumber:

NSDecimalNumber* operandOne = [NSDecimalNumber decimalNumberWithString:@"1234.5"];
NSDecimalNumber* operandTwo = [NSDecimalNumber decimalNumberWithString:@"2335"];
NSDecimalNumber* result = [operandOne decimalNumberByDividingBy:operandTwo];
NSLog(@"%@", [result descriptionWithLocale:[NSLocale currentLocale]]);

by using descriptionWithLocale: , you get the correct NSLocaleDecimalSeparator

Update 2:
I forgot about the rounding:
NSDecimalNumber supports several rounding modes.
Read the documentation of decimalNumberByRoundingAccordingToBehavior:

Thomas Zoechling
  • 34,177
  • 3
  • 81
  • 112
  • thanks for reply.i tried this NSDecimalNumber *d1 = [tmpProduct.discountPrice decimalNumberByDividingBy:tmpProduct.msrp]; but that give me exception ( here is values i got discounprice = 1234.5 and masrp is 2335) – Nnp Dec 28 '09 at 21:37
  • I updated my answer with a small example, that uses the values you pasted. Works fine here. Maybe you can post more details? Do you use Core Data/bindings etc...? – Thomas Zoechling Dec 29 '09 at 09:39
  • thanks for reply.here is more detail. i am getting values for tmpProduct.DiscountedPrice and tmpProduct.msrp as in json formate, and i use TouchJson to parse. self.msrp =[aDictionary valueForKey:kMSRP]; self.discountPrice =[aDictionary valueForKey:kDiscountPrice]; when i tried this NSDecimalNumber *d1 = [tmpProduct.discountPrice decimalNumberByDividingBy:tmpProduct.discountPrice]; it gives me exception? so i am wondering is it original data issue that i am getting from TouchJson? why can i use my tmp.DiscountedPrice w/o converting it again as you said? and i dont use any core data.. – Nnp Dec 29 '09 at 19:15
  • i found out problem. it was data problem. now i am getting price as string like "2500.00" and than i am converting like this
    
    [NSDecimalNumber decimalNumberWithString:[aDictionary valueForKey:kDiscountPrice]"];
    
    But my orginal problem is still there if i have percentage = 45.72, it should get rounded to 46% , or if its 44.32 then 44%?
    – Nnp Dec 29 '09 at 19:53
  • As you are dealing with prices and currencies, I would take the NSDecimalNumber route. I just updated my original answer with a link to the "Rounding Off" section of Apple's documentation. – Thomas Zoechling Dec 29 '09 at 20:39
1

In this specific case, I'd suggest setting the style to NSNumberFormatterPercentStyle and the maximum fraction digits to 0. Then you shouldn't have to worry about the rounding. This works for me:

double p = d / m;  // note: not multiplying by 100 here

NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
[formatter setNumberStyle:NSNumberFormatterPercentStyle];
NSString *s = [formatter stringFromNumber:[NSNumber numberWithDouble:p]];

This gives the string "45%".

jasoncrawford
  • 2,713
  • 2
  • 21
  • 20