convert number to words ,in objective c
I need to write n开发者_开发知识库umbers like: 1>yek
123>yeksad o bist o se
1,123>yek hezar o yeksad o bist o se
3,002,001>se milion o do hezar o yek ...
I,m beginner in objective c :)
I write it in c but I want to convert it ,into obj_c! How can i do that?
somthing like:
const char *yekan[10]={"","yek","do","se","chahar","panj","shesh","haft","hasht","noh"};
char ary[9]={'0','0','0','0','0','0','0','0','0'};
// get a number from user & converting it to string
// user number: 123 > the number in ary:000 000 123)
// '3' => 3
m=(int) ary[8];
m=m-'0';
if (j==3) { printf(" %s ",yekan[m]);} // yekan[3] = se
output: se
thanks.
NSNumberFormatter class has built-in functionality for conversion numbers to words:
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
[formatter setNumberStyle: NSNumberFormatterSpellOutStyle];
[formatter setLocale:...]; // Set locale if you want to use something other then the current one
NSString* numberString = [formatter stringFromNumber:[NSNumber numberWithInt: 100]];
Swift version of @Vladimir answer (Just in case)
let formatter = NSNumberFormatter()
formatter.numberStyle = .SpellOutStyle
formatter.locale = NSLocale(localeIdentifier: "en_US")
let string = formatter.stringFromNumber(55015515)
精彩评论