从一分到千万亿,下面是js的实现,其他语言改一改就好
//入参形如`100`, `100.00`
amountCaptial(amountStr) {
amountStr = amountStr + (amountStr.indexOf('.') >= 0 ? '0000' : '.0000');
let caps = ['零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖'];
let units = ['', '拾', '佰', '仟',];
let fpt = amountStr.substr(amountStr.indexOf('.') + 1);
let parseKilo = function (num, unit/*单位*/, hasContext/*是否有上下文(加0)*/, forceUnit) {
let position = 0;
let result = '';
for (; num >= 1; num /= 10, position++) {
let oct = parseInt(num % 10);
if (oct !== 0)
result = caps[oct] + units[position] + result;
else if (result.length > 0 && result.charAt(0) !== '零')
result = '零' + result;
}
return (
result.length > 0
? ((num < 1000 && hasContext ? '零' : '') + result + unit)
: (forceUnit ? unit : '')
);
};
return (
parseKilo((amountStr / 1e12) % 1e4, '万', false, false)//万亿
+ parseKilo((amountStr / 1e8) % 1e4, '亿', amountStr >= 1e12, amountStr >= 1e12)//亿
+ parseKilo((amountStr / 1e4) % 1e4, '万', amountStr >= 1e8)//万
+ parseKilo((amountStr) % 1e4, '', amountStr >= 1e4)//千
+ '圆'
+ (fpt.charAt(0) !== '0' ? caps[parseInt(fpt.charAt(0))] + '角' : '')
+ (fpt.charAt(1) !== '0' ? caps[parseInt(fpt.charAt(1))] + '分' : '')
+ (fpt.charAt(1) === '0' ? '整' : '')
);
}