We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
const cnNums = ['零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖']; const pos4OccurMap = { 1: '', 2: '万', 3: '亿', }; const Zero = '零'; function fmtNum(numStr) { let StrTxt = ''; let pos4Occur = 0; const prependTxt = (a) => { StrTxt = a + StrTxt; }; for (let indexFromLast = 0; indexFromLast < numStr.length; indexFromLast++) { const curIndex = numStr.length - 1 - indexFromLast; const curItem = numStr[curIndex]; const pos4 = indexFromLast % 4; if (pos4 === 0) { pos4Occur += 1; } const fmtNormal = (name) => { const nextItem = numStr[numStr.length - indexFromLast]; if (parseInt(curItem) === 0) { if (parseInt(nextItem) !== 0) { prependTxt(Zero); } } else { prependTxt(cnNums[curItem] + name); } }; switch (pos4) { case 0: { if (curItem !== '0') { prependTxt(cnNums[curItem] + pos4OccurMap[pos4Occur]); } else if ( numStr[curIndex] === '0' && numStr[curIndex - 1] === '0' && numStr[curIndex - 2] === '0' && numStr[curIndex - 3] === '0' ) { if (typeof numStr[curIndex + 1] !== 'undefined' && numStr[curIndex + 1] !== '0') { prependTxt(Zero); } } else { prependTxt(pos4OccurMap[pos4Occur]); } break; } case 1: { fmtNormal('拾'); break; } case 2: { fmtNormal('佰'); break; } case 3: { fmtNormal('仟'); break; } default: break; } } return StrTxt; } function fmtDecimal(numStr, showZero) { if (numStr.length === 0) { return ''; } if (numStr.length === 1) { return `${cnNums[numStr[0]]}角`; } let desTxt = ''; if (numStr[0] === '0') { if (showZero === true) { desTxt += Zero; } } else { desTxt += `${cnNums[numStr[0]]}角`; } if (numStr[1] !== '0') { desTxt += cnNums[numStr[1]]; desTxt += '分'; } return desTxt; } function toRmb(a) { const [partInt = '', partDes = ''] = a.split('.'); if (parseInt(partInt, 10) === 0 && parseInt(partDes, 10) === 0) { return '零元整'; } if (partInt === '0' && partDes) { return fmtDecimal(partDes, false); } if (!partDes || parseInt(partDes, 10) === 0) { return `${fmtNum(partInt)}元整`; } return `${fmtNum(partInt)}元${fmtDecimal(partDes, true)}`; } module.exports = toRmb;
The text was updated successfully, but these errors were encountered:
No branches or pull requests
The text was updated successfully, but these errors were encountered: