改编自:https://blog.csdn.net/kshine2017/article/details/79408807
QString JCode::toChineseBIG(const double &Fnumber,QString unit) { //转中文大写数字,unit 是单位: 元 公斤 吨 等等! if(qAbs(Fnumber)<0.01) //保留2位小数时,近似看作零的情况 return "零"; //判断正负号 QString numberSign;//存储符号 if(Fnumber<0) numberSign = "(负数)"; //将数据的绝对值 转换成字符串,如-58 转成 “58.00” QString number = QString::number(qAbs(Fnumber),'f',2);//qAbs绝对值 ,保留两位小数 QString Left_of_Point;//整数部分 int length =number.length()-3;//整数部分的长度,(精确度为2,去除小数和小数点) if(length>12) { //qDebug()<<"输入的数值超过范围!" return "输入的数值超过范围!"; } QStringList numerical_unit = {"","拾","佰","仟","万","拾", "佰", "仟", "亿", "拾", "佰", "仟"}; QStringList numerical_value = {"零","壹","贰","叁","肆","伍","陆","柒","捌","玖"}; //数数整数低位多少个连零 int counter_0=0; for(int i =length-1;i>=0;i--) { if((number[i].toLatin1()-'0' )==0) //从个位开始,向高位走 counter_0++; else break; } if(length==1 && counter_0==1) //0.x counter_0=0; //不进行过滤 for(int i=0,flag=1;i<length-counter_0;i++) { if((number[i].toLatin1()-'0')==0) { if((flag!=0 && (length-1-i)%4 != 0) || length ==1) //flag!=0 表示前一个数值 不为0 Left_of_Point+="零"; //后面不用添加 单位 if((length-1-i)%4 == 0) //如果0处于分段处,后面需添加单位 Left_of_Point+=numerical_unit[length-1-i];//添加数值单位 flag =0; //标记 } else { flag =1; Left_of_Point+=numerical_value[number[i].toLatin1()-'0']; //'5'-'0'==5 Left_of_Point+=numerical_unit[length-1-i];//添加数值单位 } } //QString Right_of_Point;//小数点右侧,小数部分(保留两位) xxxx.yy int totalLength = number.length(); if(number[totalLength-2]=='0'&&number[totalLength-1]=='0') { QString Bigcn=numberSign+Left_of_Point+unit; return Bigcn; } else if(number[totalLength-2]!='0'&&number[totalLength-1]=='0') { QString Bigcn=numberSign+Left_of_Point+"点"+numerical_value[number[totalLength-2].toLatin1()-'0']+unit; return Bigcn; } else { QString Bigcn=numberSign+Left_of_Point+"点"+numerical_value[number[totalLength-2].toLatin1()-'0']+numerical_value[number[totalLength-1].toLatin1()-'0']+unit; return Bigcn; } return "异常情况"; }
发表评论