Skip to content
New issue

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

js格式化RMB #16

Open
21paradox opened this issue Jan 2, 2019 · 0 comments
Open

js格式化RMB #16

21paradox opened this issue Jan 2, 2019 · 0 comments

Comments

@21paradox
Copy link
Owner

21paradox commented Jan 2, 2019

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;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant