Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

formatting value in a string to have a decimal separator

if i have a value stored like:

long long unsigned x=1000000000000;

i'd like to have a

NSString *strx=[...];

so if i use NSLog or bitmapfont for this integer, what is displayed will be:

1,000,000,000,000

do exists this king od formatter? thanks

like image 644
sefiroths Avatar asked Jan 19 '23 02:01

sefiroths


1 Answers

NSNumberFormatter is the class to go to. For example:

long num = 1000000000;
NSNumberFormatter *fmt = [[NSNumberFormatter alloc] init];
fmt.positiveFormat = @"#,###";
NSLog(@"%@", [fmt stringFromNumber:[NSNumber numberWithLong:num]]);

prints out 1,000,000,000. There are a lot of predefined styles and further options that can be explored:

  • NSNumberFormatter reference: http://developer.apple.com/library/ios/#documentation/Cocoa/Reference/Foundation/Classes/NSNumberFormatter_Class/Reference/Reference.html
  • Format patterns: http://unicode.org/reports/tr35/tr35-10.html#Number_Format_Patterns
like image 147
Dennis Bliefernicht Avatar answered Jan 29 '23 14:01

Dennis Bliefernicht