在開發(fā)中遇到一個需求,就是需要將阿拉伯?dāng)?shù)字轉(zhuǎn)成漢字?jǐn)?shù)字,或者將漢字?jǐn)?shù)字轉(zhuǎn)成阿拉伯?dāng)?shù)字
比如:123---> 一百二十三 321--->三百二十一
直接上代碼
1马昙,阿拉伯?dāng)?shù)字轉(zhuǎn)成漢字?jǐn)?shù)
- (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栓辜,漢字?jǐn)?shù)字轉(zhuǎn)成阿拉伯?dāng)?shù)字
- (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表示正數(shù),no表示負(fù)數(shù)
NSString * s=[arabic substringWithRange:NSMakeRange(0, 1)];
if([s isEqualToString:@"負(fù)"]){
flag=NO;
}
int i=0;
if(!flag){
i=1;
}
int sum=0;//和
int num[20];//保存單個漢字信息數(shù)組
for(int i=0;i<20;i++){//將其全部賦值為0
num[i]=0;
}
int k=0;//用來記錄數(shù)據(jù)的個數(shù)
//如果是負(fù)數(shù)隧土,正常的數(shù)據(jù)從第二個漢字開始提针,否則從第一個開始
for(;i<[arabic length];i++){
NSString * key=[arabic substringWithRange:NSMakeRange(i, 1)];
int tmp=[[mdic valueForKey:key] intValue];
num[k++]=tmp;
}
//將獲得的所有數(shù)據(jù)進(jìn)行拼裝
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){//如果正數(shù)
NSLog(@"%d",sum);
result=[NSMutableString stringWithFormat:@"%d",sum];
}else{//如果負(fù)數(shù)
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);