QString Invoice::AmountsConverted(double amount)
{
QStringList cnNumber = {"零","壹","贰","叁","肆","伍","陆","柒","捌","玖"};
// 拆分整数与小数部分
QString strAmount, strInteger, strDecimal; // 金额字符串, 整数部分, 小数部分
strAmount.setNum(amount, 'f', 2);
strInteger = strAmount.split('.').at(0);
strDecimal = strAmount.split('.').at(1);
QStringList strlInteger, strlDecimal;
strlInteger = strInteger.split("");
strlInteger.removeFirst(); strlInteger.removeLast(); //移除首尾空值
strlDecimal = strDecimal.split("");
strlDecimal.removeFirst(); strlDecimal.removeLast(); //移除首尾空值
// 构建汉字列表
QStringList strlCnInteger, strlCnDecimal;
foreach (QString str, strlInteger) {
strlCnInteger << cnNumber.at(str.toInt());
}
foreach (QString str, strlDecimal) {
strlCnDecimal << cnNumber.at(str.toInt());
}
// 整数部分添加单位
int nCount = strlInteger.size() - 1; // 总共多少位
bool yflag = false, sflag = false, bflag = false, qflag = false, wflag = false;
int nStep = 0;
for(int i = nCount; i >= 0; i--)
{
switch (nStep++)
{
case 0: // 元
if (strlInteger.at(i) == "0") strlCnInteger.removeAt(i);
else
yflag = true;
break;
case 1: // 拾
if (strlInteger.at(i) == "0")
{
if (!yflag)
strlCnInteger.removeAt(i);
}
else if (strlInteger.at(i) != "0")
strlCnInteger.insert(i + 1, "拾"), sflag = true;
break;
case 2: // 百
if (strlInteger.at(i) == "0")
{
if (!yflag && !sflag)
strlCnInteger.removeAt(i);
if (yflag && !sflag)
strlCnInteger.removeAt(i);
}
else if (strlInteger.at(i) != "0")
strlCnInteger.insert(i + 1, "佰"), bflag = true;
break;
case 3: // 仟
if (strlInteger.at(i) == "0")
{
if (!yflag && !sflag && !bflag)
strlCnInteger.removeAt(i);
if ((yflag || sflag) && !bflag)
strlCnInteger.removeAt(i);
}
else if (strlInteger.at(i) != "0")
strlCnInteger.insert(i + 1, "仟"), qflag = true;
break;
case 4: // 万
if (!wflag)
{
if (strlInteger.at(i) == "0")
{
if (!qflag)
strlCnInteger.removeAt(i);
strlCnInteger.insert(i, "万");
yflag = false;
wflag = true;
}
else
strlCnInteger.insert(i + 1, "万"), yflag = true, wflag = true;
}
else // 亿
{
if (strlInteger.at(i) == "0")
{
strlCnInteger.removeAt(i);
strlCnInteger.insert(i, "亿");
yflag = false;
}
else
strlCnInteger.insert(i + 1, "亿"), yflag = true;
}
nStep = 1; sflag = bflag = qflag = false;
break;
}
}
// 小数部分添加单位
// 分
if (strlDecimal.at(1) == "0")
{
strlCnDecimal.removeAt(1);
}
else
{
strlCnDecimal.insert(2, "分");
}
// 角
if (strlDecimal.at(0) == "0")
{
if (strlDecimal.at(1) == "0")
strlCnDecimal.removeAt(0);
}
else
{
strlCnDecimal.insert(1, "角");
}
QString result = strlCnInteger.join("") + "圆" + strlCnDecimal.join("") + "整";
return result;
}
C++方式实现人民币小写转大写
最新推荐文章于 2021-07-25 07:11:50 发布