一.将阿拉伯数字转换成汉文数字
- (NSString *)translationArabicNum:(NSInteger)arabicNum
{
NSString *arabicNumStr = [NSString stringWithFormat:@"%ld",(long)arabicNum];
NSArray *arabicNumeralsArray = @[@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9",@"0"];
NSArray *chineseNumeralsArray = @[@"一",@"二",@"三",@"四",@"五",@"六",@"七",@"八",@"九",@"零"];
NSArray *digits = @[@"个",@"十",@"百",@"千",@"万",@"十",@"百",@"千",@"亿",@"十",@"百",@"千",@"兆"];
NSDictionary *dictionary = [NSDictionary dictionaryWithObjects:chineseNumeralsArray forKeys:arabicNumeralsArray];
if (arabicNum < 20 && arabicNum > 9) {
if (arabicNum == 10) {
return @"十";
}else{
NSString *subStr1 = [arabicNumStr substringWithRange:NSMakeRange(1, 1)];
NSString *a1 = [dictionary objectForKey:subStr1];
NSString *chinese1 = [NSString stringWithFormat:@"十%@",a1];
return chinese1;
}
}else{
NSMutableArray *sums = [NSMutableArray array];
for (int i = 0; i < arabicNumStr.length; i ++)
{
NSString *substr = [arabicNumStr substringWithRange:NSMakeRange(i, 1)];
NSString *a = [dictionary objectForKey:substr];
NSString *b = digits[arabicNumStr.length -i-1];
NSString *sum = [a stringByAppendingString:b];
if ([a isEqualToString:chineseNumeralsArray[9]])
{
if([b isEqualToString:digits[4]] || [b isEqualToString:digits[8]])
{
sum = b;
if ([[sums lastObject] isEqualToString:chineseNumeralsArray[9]])
{
[sums removeLastObject];
}
}else
{
sum = chineseNumeralsArray[9];
}
if ([[sums lastObject] isEqualToString:sum])
{
continue;
}
}
[sums addObject:sum];
}
NSString *sumStr = [sums componentsJoinedByString:@""];
NSString *chinese = [sumStr substringToIndex:sumStr.length-1];
return chinese;
}
}
二.将日期转换成古月份
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
// 初始化时间格式控制器
NSDateFormatter *matter = [[NSDateFormatter alloc] init];
// 设置设计格式
[matter setDateFormat:@"MM月dd日"];
// 进行转换
NSString *totalStr = [matter stringFromDate:[NSDate date]];
NSLog(@"今天是%@",totalStr);
//截取月份
NSString *monStr = [totalStr substringWithRange:NSMakeRange(0, 2)];
NSLog(@"今天是%@月",monStr);
//截取日号
NSString *dateStr = [totalStr substringWithRange:NSMakeRange(3, 2)];
NSLog(@"今天是%@日",dateStr);
NSString *dae = [self translationArabicNum:[monStr integerValue]];
NSLog(@"%@",dae);
}
- (NSString *)translationArabicNum:(NSInteger)arabicNum
{
NSString *arabicNumStr = [NSString stringWithFormat:@"%ld",(long)arabicNum];
NSArray *arabicNumeralsArray = @[@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9",@"10",@"11",@"12"];
NSArray *chineseNumeralsArray = @[@"壹",@"贰",@"叁",@"肆",@"伍",@"陆",@"柒",@"捌",@"玖",@"拾",@"冬",@"腊"];
NSDictionary *dictionary = [NSDictionary dictionaryWithObjects:chineseNumeralsArray forKeys:arabicNumeralsArray];
NSString *chinese = [dictionary objectForKey:arabicNumStr];
return chinese;
}
网友评论