开发者

IS there any easier way to show $ currency format?(As specially commas) Can I integrate RE with Objective C code?

开发者 https://www.devze.com 2023-04-01 08:47 出处:网络
I using X code 4. And Making an US Currency Calculator. My Aim is to display the numbers in proper format?

I using X code 4.

And Making an US Currency Calculator.

My Aim is to display the numbers in proper format?

Like

$ 1,234

$12,100

$1,23,123

I dont want do this by appendi开发者_运维问答ng , and string stuff.

Is there any easier way so that I can integrate any RE or any ruby or Python. Which will help me to make currency format.

Please show me easier way rather than long programming.


The NSNumberFormatter allows you to format currency. Example taken from [iOS developer:tips];

// Create formatter
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];  
//--------------------------------------------
// Format style as currency, output to console
//--------------------------------------------
[formatter setNumberStyle:NSNumberFormatterCurrencyStyle];
NSString *formattedOutput = [formatter stringFromNumber:[NSNumber numberWithInt:1234]];
NSLog(@"Output as currency: %@", formattedOutput);

//--------------------------------------------
// Change local and output as currency
//--------------------------------------------
NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:@"it_IT"];
[formatter setLocale:locale];
formattedOutput = [formatter stringFromNumber:[NSNumber numberWithInt:1234]];
NSLog(@"Output as currency - locale it_IT: %@", formattedOutput);

[locale release];
[formatter release];


Found this answer here: Objective-C: How to format string as $ Price

NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
[formatter setNumberStyle:NSNumberFormatterCurrencyStyle];
... = [formatter stringFromNumber:number];

By default it uses the locale of your system, but you can change that and lots of other properties, see the NSNumberFormatter API docs.

0

精彩评论

暂无评论...
验证码 换一张
取 消