iOS人民币阿拉伯数字转大写汉字

[objc]
– (NSString*)conversionChineseDigital:(NSString*)amountStr{

NSArray *unitAr = @[@"分", @"角", @"元", @"拾", @"佰", @"仟", @"万", @"拾", @"佰", @"仟", @"亿"];
NSArray *numAr = @[@"零", @"壹", @"贰", @"叁", @"肆", @"伍", @"陆", @"柒", @"捌", @"玖"];

//整理字符串,长度
amountStr = [NSString stringWithFormat:@"%.2f",[amountStr doubleValue]];
amountStr = [amountStr stringByReplacingOccurrencesOfString:@"." withString:@""];

int length = amountStr.length;

NSMutableString *tempStr = [NSMutableString new];
BOOL zero = NO;

for (int i = 0; i < length; i++) {

NSString *tempNum = [amountStr substringWithRange:NSMakeRange(i ,1)];

//判断“零”串
if ([tempNum intValue] == 0){
zero = YES;

//出“零”,单位“分“,不显示
if ( i == length – 2 ) {
zero = NO;
continue;
}

//出“零”,补“元”,去“零”
if ( i == length – 3 ) {

//出“零”,单位“元”,不显示
if ( i == 0 ) {
zero = NO;
continue;
}

[tempStr appendString:@"元"];
zero = NO;
continue;
}

//补“万”,倒数第7个
if ( i == length – 7 ) {
[tempStr appendString:@"万"];
continue;
}

continue;

}else{

//非“零”,且之前出现“零”,补“零”
if (zero == YES) {
[tempStr appendString:@"零"];
zero = NO;
}
}

//改"拾",首位为“壹”,且位置为“拾”位
if ( i == 0 && (length == 4 || length == 8) && [tempNum intValue] == 1) {
[tempStr appendString:@"拾"];

continue;
}

[tempStr appendString:[numAr objectAtIndex:[tempNum intValue]]];

//补“元”,倒数第3个
if ( i == length – 3 ) {

[tempStr appendString:@"元"];
continue;
}

//补“万”,倒数第7个
if ( i == length – 7 ) {
[tempStr appendString:@"万"];
continue;
}

[tempStr appendString:[unitAr objectAtIndex:(length – i – 1)]];

}

//补“整”,如无“角”,“分”补“整”
if ([tempStr hasSuffix:@"元"]) {
[tempStr appendString:@"整"];
}

NSLog(@">>%@ %@",tempStr,amountStr);

return tempStr;
}
[/objc]

72 thoughts on “iOS人民币阿拉伯数字转大写汉字

  1. visitor

    中華職棒直播台灣球迷的首選官方認證資訊平台,24小時不間斷提供官方中華職棒直播新聞、球員數據分析,以及精準的比賽預測。

Xafbxa进行回复 取消回复

电子邮件地址不会被公开。