0

I have UILabel in which i show float value i want that if value is in thousand like $1000.00 then label should show $1,000.00

Using following way my code shows

$1000.00

        labelOne.text=[NSString stringWithFormat:@"$ %.2f",f_Cost_of_Vacination_NV];

4 Answers4

0

You'll need to use NSNumberFormatter

NSString *formatted = [NSNumberFormatter localizedStringFromNumber:@(f_Cost_of_Vacination_NV) numberStyle:NSNumberFormatterCurrencyStyle];
NSLog(@"%@", formatted); // will log it as 1,000.00
Ahmed Z.
  • 2,329
  • 23
  • 52
0

You need to use the Number style Currency.this might solve your problem use NSNumberFormatter... and use this line to set currency style

NSNumberFormatterCurrencyStyle.

See this

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


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

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

    NSLog(@"%@",text);
Jitendra
  • 5,055
  • 2
  • 22
  • 42
0

You can do like this :

double currency=1000;
    NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init] ;
    [numberFormatter setNumberStyle: NSNumberFormatterCurrencyStyle];
    NSString *numberAsString = [numberFormatter stringFromNumber:[NSNumber numberWithInt:currency]];
    NSLog(@" print Converted:%@",numberAsString]);

Happy Coding !

Sawant
  • 395
  • 6
  • 22
  • There is no need to use `stringWithFormat` in an `NSLog`. Just do: `NSLog(@" print Converted: %@", numberAsString);` – rmaddy Aug 21 '13 at 06:36
  • Huh? You don't need the embedded `stringWithFormat`. `NSLog` fully support string formats. It's pointless to use `stringWithFormat` in `NSLog`. – rmaddy Aug 21 '13 at 06:42
  • No need for an apology. It seems you changed your previous comment. I'm just trying to be helpful and explain why don't need the extra `stringWithFormat`. Enjoy. – rmaddy Aug 21 '13 at 06:47
-1

try this one

    float value = 200000;
    NSNumberFormatter * formatter = [NSNumberFormatter new];
    [formatter setNumberStyle:NSNumberFormatterDecimalStyle];
    [formatter setMaximumFractionDigits:2]; // Set this if you need 2 digits
    NSString * newString =  [formatter stringFromNumber:[NSNumber numberWithFloat:value]];

 labelOne.text=[NSString stringWithFormat:@"$ %@",newString];
nitin kachhadiya
  • 959
  • 2
  • 9
  • 21