iOS---------金额转大写
-(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];
}
上一篇: 吃什么不会发胖,你还在踩坑吗?
下一篇: 01 js数据类型