paper-helper.js 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. const chnNumChar = ["零", "一", "二", "三", "四", "五", "六", "七", "八", "九"];
  2. const chnUnitSection = ["", "万", "亿", "万亿", "亿亿"];
  3. const chnUnitChar = ["", "十", "百", "千"];
  4. export const sectionToChinese = function (section) {
  5. let strIns = "", chnStr = "";
  6. let unitPos = 0;
  7. let zero = true;
  8. while (section > 0) {
  9. const v = section % 10;
  10. if (v === 0) {
  11. if (!zero) {
  12. zero = true;
  13. chnStr = chnNumChar[v] + chnStr;
  14. }
  15. } else {
  16. zero = false;
  17. strIns = chnNumChar[v];
  18. strIns += chnUnitChar[unitPos];
  19. chnStr = strIns + chnStr;
  20. }
  21. unitPos++;
  22. section = Math.floor(section / 10);
  23. }
  24. return chnStr;
  25. }
  26. export const numberToChinese = function (num) {
  27. let unitPos = 0;
  28. let strIns = "", chnStr = "";
  29. let needZero = false;
  30. if (num === 0) {
  31. return chnNumChar[0];
  32. }
  33. while (num > 0) {
  34. const section = num % 10000;
  35. if (needZero) {
  36. chnStr = chnNumChar[0] + chnStr;
  37. }
  38. strIns = sectionToChinese(section);
  39. strIns += section !== 0 ? chnUnitSection[unitPos] : chnUnitSection[0];
  40. chnStr = strIns + chnStr;
  41. needZero = section < 1000 && section > 0;
  42. num = Math.floor(num / 10000);
  43. unitPos++;
  44. }
  45. return chnStr;
  46. }