数字转换人民币大写形式:
银行、单位和个人填写的各种票据和结算凭证需要将数字金额写成人民币大写形式,它的规则主要有以下几条:
1、数字和单位使用壹、贰、叁、肆、伍、陆、柒、捌、玖、拾、佰、仟、万、亿、元、角、分、零、整等字样,不能使用其他汉字;
2、中文大写金额数字到"元"为止的,在"元"之后、应写"整"字;在"角"之后,可以不写"整"字;大写金额数字有"分"的,"分"后面不写"整"字;
3、数字中间有0的,要用汉字零,当数字中间有多个0时,人民币大写形式按口语习惯只保留一个零,如数字30001对应转换结果为“叁万零壹元整”
输入一个数字(长度不超过10位,可能包含2位小数)。
输出
对应人民币大写形式。
样例输入
123456789.26
样例输出
壹亿贰仟叁佰肆拾伍万陆仟柒佰捌拾玖元贰角伍分
最近做一个题,把我自己的写法放这里吧
#include <stdio.h>
const char temp1[10][10] = {"零","壹","贰","叁","肆","伍","陆","柒","捌","玖"};
const char unit[10][10] = {"","拾","佰","仟"};
int getstr(char* s,char *value)
{
// printf("input %s\n",s);
int zeroFlag = 0;
int len = strlen(s);
for(int i = 0;i < len;i ++)
{
if(s[i]>'0')
{
if(zeroFlag == 0)
{
strcat(value,temp1[s[i]-'0']);
strcat(value,unit[len - i -1]);
}
else
{
strcat(value,temp1[0]);
strcat(value,temp1[s[i]-'0']);
strcat(value,unit[len - i -1]);
zeroFlag = 0;
}
}
else
{
zeroFlag ++;
}
}
if(zeroFlag == len)
{
return -1;
}
return 0;
}
int main()
{
double money,small;
int small_int;
while(scanf("%lf",&money)!= EOF)
{
if(money < 1 && money > 1e-6)
{
small_int = (money+0.005)*100;
if(small_int/10 ==0)
printf("%s分\n",temp1[small_int%10]);
else if(small_int%10 ==0)
printf("%s角\n",temp1[small_int/10]);
else
printf("%s角%s分\n",temp1[small_int/10],temp1[small_int%10]);
continue;
}
// printf("money %lf\n",money);
char value[256]={0};
char strM[32] = {0};
char ss[16]={0};
int ps = 0;
sprintf(strM,"%d",(int)money);
int flag = strlen(strM)/4;
int firstsize = strlen(strM)%4;
for(int i = 0;i < strlen(strM); i++)
{
ss[ps++]=strM[i];
if(ps == 4 || (firstsize != 0 && ps == firstsize))
{
if(getstr(ss,value) == 0)
{
if(flag==0)
{
strcat(value,"元");
}
if(flag==1)
{
strcat(value,"万");
}
if(flag==2)
{
strcat(value,"亿");
}
}
else
{
strcat(value,"元");
}
flag --;
firstsize =0;
ps =0;
}
}
small = (money -(int)money) + 0.005;
if( small < 0.01) //判断是否为整
strcat(value,"整");
else //为xxx.xx
{
small_int = small*100;
//printf("sammm %d\n",small_int);
if(small_int/10 ==0)
{
char temp[32]= {0};
sprintf(temp,"零%s分\n",temp1[small_int%10]);
strcat(value,temp);
}
else if(small_int%10 ==0)
{
char temp[32]= {0};
sprintf(temp,"%s角\n",temp1[small_int/10]);
strcat(value,temp);
}
else
{
char temp[32]= {0};
sprintf(temp,"%s角%s分\n",temp1[small_int/10],temp1[small_int%10]);
strcat(value,temp);
}
}
printf("%s",value);
}
return 0;
}