方法一
在开发中遇到一个需求,就是需要将阿拉伯数字转成汉字数字,或者将汉字数字转成阿拉伯数字
比如:123---> 一百二十三 321--->三百二十一
直接上代码
1、阿拉伯数字转成汉字数
- (NSString *)translation:(NSString *)arebic
{ NSString *str = arebic;
NSArray *arabic_numerals = @[@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9",@"0"];
NSArray *chinese_numerals = @[@"一",@"二",@"三",@"四",@"五",@"六",@"七",@"八",@"九",@"零"];
NSArray *digits = @[@"个",@"十",@"百",@"千",@"万",@"十",@"百",@"千",@"亿",@"十",@"百",@"千",@"兆"];
NSDictionary *dictionary = [NSDictionary dictionaryWithObjects:chinese_numerals forKeys:arabic_numerals];
NSMutableArray *sums = [NSMutableArray array];
for (int i = 0; i < str.length; i ++) {
NSString *substr = [str substringWithRange:NSMakeRange(i, 1)];
NSString *a = [dictionary objectForKey:substr];
NSString *b = digits[str.length -i-1];
NSString *sum = [a stringByAppendingString:b];
if ([a isEqualToString:chinese_numerals[9]])
{
if([b isEqualToString:digits[4]] || [b isEqualToString:digits[8]])
{
sum = b;
if ([[sums lastObject] isEqualToString:chinese_numerals[9]])
{
[sums removeLastObject];
}
}else
{
sum = chinese_numerals[9];
}
if ([[sums lastObject] isEqualToString:sum])
{
continue;
}
}
[sums addObject:sum];
}
NSString *sumStr = [sums componentsJoinedByString:@""];
NSString *chinese = [sumStr substringToIndex:sumStr.length-1];
// NSLog(@"str %@",str);
NSLog(@"%s %@", __func__, chinese);
return chinese;
}
2、汉字数字转成阿拉伯数字
- (NSString *)arabicNumberalsFromChineseNumberals:(NSString *)arabic{
NSMutableDictionary * mdic =[[NSMutableDictionary alloc]init];
[mdic setObject:[NSNumber numberWithInt:10000] forKey:@"万"];
[mdic setObject:[NSNumber numberWithInt:1000] forKey:@"千"];
[mdic setObject:[NSNumber numberWithInt:100] forKey:@"百"];
[mdic setObject:[NSNumber numberWithInt:10] forKey:@"十"];
[mdic setObject:[NSNumber numberWithInt:9] forKey:@"九"];
[mdic setObject:[NSNumber numberWithInt:8] forKey:@"八"];
[mdic setObject:[NSNumber numberWithInt:7] forKey:@"七"];
[mdic setObject:[NSNumber numberWithInt:6] forKey:@"六"];
[mdic setObject:[NSNumber numberWithInt:5] forKey:@"五"];
[mdic setObject:[NSNumber numberWithInt:4] forKey:@"四"];
[mdic setObject:[NSNumber numberWithInt:3] forKey:@"三"];
[mdic setObject:[NSNumber numberWithInt:2] forKey:@"二"];
[mdic setObject:[NSNumber numberWithInt:2] forKey:@"两"];
[mdic setObject:[NSNumber numberWithInt:1] forKey:@"一"];
[mdic setObject:[NSNumber numberWithInt:0] forKey:@"零"];
// NSLog(@"%@",mdic);
BOOL flag=YES;//yes表示正数,no表示负数
NSString * s=[arabic substringWithRange:NSMakeRange(0, 1)];
if([s isEqualToString:@"负"]){
flag=NO;
}
int i=0;
if(!flag){
i=1;
}
int sum=0;//和
int num[20];//保存单个汉字信息数组
for(int i=0;i<20;i++){//将其全部赋值为0
num[i]=0;
}
int k=0;//用来记录数据的个数
//如果是负数,正常的数据从第二个汉字开始,否则从第一个开始
for(;i<[arabic length];i++){
NSString * key=[arabic substringWithRange:NSMakeRange(i, 1)];
int tmp=[[mdic valueForKey:key] intValue];
num[k++]=tmp;
}
//将获得的所有数据进行拼装
for(int i=0;i<k;i++){
if(num[i]<10&&num[i+1]>=10){
sum+=num[i]*num[i+1];
i++;
}else{
sum+=num[i];
}
}
NSMutableString * result=[[NSMutableString alloc]init];;
if(flag){//如果正数
NSLog(@"%d",sum);
result=[NSMutableString stringWithFormat:@"%d",sum];
}else{//如果负数
NSLog(@"-%d",sum);
result=[NSMutableString stringWithFormat:@"-%d",sum];
}
NSLog(@"%s %@", __func__, result);
return result;
}
测试用例
NSString *chinese = [self translation:@"456"];
NSLog(@"%@",chinese);
NSString *num = [self arabicNumberalsFromChineseNumberals:@"一百五十一"];
NSLog(@"%@", num);
方法二:系统方法
通过NSNumberFormatter,可以设置NSNumber输出的格式。例如如下代码:
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
formatter.numberStyle = NSNumberFormatterDecimalStyle;
NSString *string = [formatter stringFromNumber:[NSNumber numberWithInt:123456789]];
NSLog(@"Formatted number string:%@",string);
输出结果为:[1223:403] Formatted number string:123,456,789
其中NSNumberFormatter类有个属性numberStyle,它是一个枚举型,设置不同的值可以输出不同的数字格式。该枚举包括:
enum {
NSNumberFormatterNoStyle = kCFNumberFormatterNoStyle,
NSNumberFormatterDecimalStyle = kCFNumberFormatterDecimalStyle,
NSNumberFormatterCurrencyStyle = kCFNumberFormatterCurrencyStyle,
NSNumberFormatterPercentStyle = kCFNumberFormatterPercentStyle,
NSNumberFormatterScientificStyle = kCFNumberFormatterScientificStyle,
NSNumberFormatterSpellOutStyle = kCFNumberFormatterSpellOutStyle
NSNumberFormatterRoundCeiling = kCFNumberFormatterRoundCeiling,//四舍五入,原值2.7999999999,直接输出3
NSNumberFormatterRoundFloor = kCFNumberFormatterRoundFloor,//保留小数输出2.8 正是想要的
NSNumberFormatterRoundDown = kCFNumberFormatterRoundDown,//加上了人民币标志,原值输出¥2.8
NSNumberFormatterRoundUp = kCFNumberFormatterRoundUp,//本身数值乘以100后用百分号表示,输出280%
NSNumberFormatterRoundHalfEven = kCFNumberFormatterRoundHalfEven,//原值表示,输出2.799999999E0
NSNumberFormatterRoundHalfDown = kCFNumberFormatterRoundHalfDown,//原值的中文表示,输出二点七九九九。。。。
NSNumberFormatterRoundHalfUp = kCFNumberFormatterRoundHalfUp //原值中文表示,输出第三
};
typedef NSUInteger NSNumberFormatterStyle;
各个枚举对应输出数字格式的效果如下:
[1243:403] Formatted number string:123456789
[1243:403] Formatted number string:123,456,789
[1243:403] Formatted number string:¥123,456,789.00
[1243:403] Formatted number string:-539,222,988%
[1243:403] Formatted number string:1.23456789E8
[1243:403] Formatted number string:一亿二千三百四十五万六千七百八十九
其中第三项和最后一项的输出会根据系统设置的语言区域的不同而不同