Native Developer
Native Developer

Reputation: 73

NSNumberFormatter gives wrong currency

I have UILabel in which i want to show $ also it works fine but problem is that it works on simulator correct but when i test this code on device instead $ it shows Rs (Rupess) any idea how to fix this issue.

  NSNumber *number=[[NSNumber alloc]initWithInt:1000];


   NSString *No = [NSNumberFormatter localizedStringFromNumber:number numberStyle:NSNumberFormatterCurrencyStyle];
   NSLog(@"%@", No);

   UILabel * text=[NSString stringWithFormat:@"%@",No];

   NSLog(@"%@",text);

Upvotes: 0

Views: 503

Answers (4)

Steve Wilford
Steve Wilford

Reputation: 9002

I doubt you want to set the result of [NSString stringWithFormat:@"%@",No] to a UILabel instance.

The other answers seem the most likely, choosing the correct device regional settings and using your existing code to output the value in the local format.

However if anyone is interested in outputting a number with an arbitrary currency format:

NSNumber *number = [NSNumber numberWithInt:1000];

NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
[formatter setNumberStyle:NSNumberFormatterCurrencyStyle];
[formatter setCurrencyCode:@"INR"];

NSString *text = [formatter stringFromNumber:number];

NSLog(@"%@", text);

// Release formatter as required

Though why you'd do this instead of just using the currency symbol and a basic string I'm not sure :p

NSString *text = @"₹1,000.00";

Upvotes: 1

Anurag Soni
Anurag Soni

Reputation: 1077

you got this issue because in device setting->general->international->reion format -> is INDIA

just change it to the country of your choice like if you want $ symbol then select USA

Upvotes: 0

Rushi
Rushi

Reputation: 4500

There's no issue.

  1. As you are using localizedStringFromNumber and NSNumberFormatterCurrencyStyle to format number it's giving you the number format as per your region.
  2. If you go to your simulator Setting - General - International - Region Format. It must be United State. Hence it shows $ when you run it on simulator.
  3. If you check the same thing in your device. It'll show Region Format as India. Hence, it's showing Rs. sign.
  4. If you want to see the $ sign just go ahead and change the setting in Device.

Upvotes: 1

Wain
Wain

Reputation: 119021

Because by default the formatter will use the locale specified on the device it is being run on. If you want to force a particular display you need to use setLocale: and supply the appropriate NSLocale instance.

Upvotes: 1

Related Questions