Last active
November 16, 2015 03:24
-
-
Save ygweric/e99b76014951e54132e8 to your computer and use it in GitHub Desktop.
数字转中文大写 obj-c
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
+ (NSString*)numberToChineseUppercase:(double)num{ | |
int iLen,iNum,iAddZero=0; | |
NSMutableString *szChMoney = [[NSMutableString alloc] init]; | |
NSArray *hzUnit = @[@"分",@"角",@"元",@"拾",@"佰",@"仟",@"万",@"拾",@"佰",@"仟",@"万",@"亿",@"拾",@"佰",@"仟",@"万",@"拾",@"佰",@"仟"]; | |
NSArray *hzNum = @[@"零",@"壹",@"贰",@"叁",@"肆",@"伍",@"陆",@"柒",@"捌",@"玖"]; | |
NSString *szNum = [NSString stringWithFormat:@"%18.0f",num*100]; | |
szNum = [szNum stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]; | |
iLen =(int) szNum.length; | |
if ((iLen > 18) || (iLen == 0) || num == 0) { | |
return @""; | |
} | |
for (int i = 0; i < iLen; i++) { | |
iNum = [[szNum substringWithRange:NSMakeRange(i, 1)] intValue]; | |
if (iNum == 0) { | |
iAddZero++; | |
} | |
else { | |
if (iAddZero > 0) { | |
[szChMoney appendString:@"零"]; | |
} | |
[szChMoney appendString:hzNum[iNum]]; | |
iAddZero = 0; | |
} | |
if (iNum != 0 //数字不等于0 添加单位 | |
|| iLen - i == 3 //处理个位单位 | |
|| iLen - i == 11 //处理亿位单位 | |
|| ((iLen - i + 1) % 8 == 0 && iAddZero < 4)) //处理万位单位 | |
{ | |
[szChMoney appendString:hzUnit[iLen-i-1]]; | |
} | |
} | |
if ([[szNum substringWithRange:NSMakeRange(iLen-2, 2)] isEqualToString:@"00"]) { | |
[szChMoney appendString:@"整"]; | |
} | |
NSLog(@"%@",szChMoney); | |
return szChMoney; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment