欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

iOS---------金额转大写

程序员文章站 2022-08-08 17:56:31
-(NSString *)digitUppercase:(NSString *)numstr{ double numberals=[numstr doubleValue]; NSArray *numberchar = @[@"零",@"壹",@"贰",@"叁",@"肆",@"伍",@"陆",@"柒" ......

-(nsstring *)digituppercase:(nsstring *)numstr{
double numberals=[numstr doublevalue];
nsarray *numberchar = @[@"零",@"壹",@"贰",@"叁",@"肆",@"伍",@"陆",@"柒",@"捌",@"玖"];
nsarray *inunitchar = @[@"",@"拾",@"佰",@"仟"];
nsarray *unitname = @[@"",@"万",@"亿",@"万亿"];
//金额乘以100转换成字符串(去除圆角分数值)
nsstring *valstr=[nsstring stringwithformat:@"%.2f",numberals];
nsstring *prefix;
nsstring *suffix;
if (valstr.length<=2) {
prefix=@"零元";
if (valstr.length==0) {
suffix=@"零角零分";
}
else if (valstr.length==1)
{
suffix=[nsstring stringwithformat:@"%@分",[numberchar objectatindex:[valstr intvalue]]];
}
else
{
nsstring *head=[valstr substringtoindex:1];
nsstring *foot=[valstr substringfromindex:1];
suffix = [nsstring stringwithformat:@"%@角%@分",[numberchar objectatindex:[head intvalue]],[numberchar objectatindex:[foot intvalue]]];
}
}
else
{
prefix=@"";
suffix=@"";
nsinteger flag = valstr.length - 2;
nsstring *head=[valstr substringtoindex:flag - 1];
nsstring *foot=[valstr substringfromindex:flag];
if (head.length>13) {
return@"数值太大(最大支持13位整数),无法处理";
}
//处理整数部分
nsmutablearray *ch=[[nsmutablearray alloc]init];
for (int i = 0; i < head.length; i++) {
nsstring * str=[nsstring stringwithformat:@"%x",[head characteratindex:i]-'0'];
[ch addobject:str];
}
int zeronum=0;

for (int i=0; i<ch.count; i++) {
int index=(ch.count -i-1)%4;//取段内位置
nsinteger indexloc=(ch.count -i-1)/4;//取段位置
if ([[ch objectatindex:i]isequaltostring:@"0"]) {
zeronum++;
}
else
{
if (zeronum!=0) {
if (index!=3) {
prefix=[prefix stringbyappendingstring:@"零"];
}
zeronum=0;
}
prefix=[prefix stringbyappendingstring:[numberchar objectatindex:[[ch objectatindex:i]intvalue]]];
prefix=[prefix stringbyappendingstring:[inunitchar objectatindex:index]];
}
if (index ==0 && zeronum<4) {
prefix=[prefix stringbyappendingstring:[unitname objectatindex:indexloc]];
}
}
prefix =[prefix stringbyappendingstring:@"元"];
//处理小数位
if ([foot isequaltostring:@"00"]) {
suffix =[suffix stringbyappendingstring:@"整"];
}
else if ([foot hasprefix:@"0"])
{
nsstring *footch=[nsstring stringwithformat:@"%x",[foot characteratindex:1]-'0'];
suffix=[nsstring stringwithformat:@"%@分",[numberchar objectatindex:[footch intvalue] ]];
}
else
{
nsstring *headch=[nsstring stringwithformat:@"%x",[foot characteratindex:0]-'0'];
nsstring *footch=[nsstring stringwithformat:@"%x",[foot characteratindex:1]-'0'];
suffix=[nsstring stringwithformat:@"%@角%@分",[numberchar objectatindex:[headch intvalue]],[numberchar objectatindex:[footch intvalue]]];
}
}
return [prefix stringbyappendingstring:suffix];
}