数字序号转为汉字序号

const num = ["", "一", "二", "三", "四", "五", "六", "七", "八", "九"];
const unit = ["","十", "百","千"];

const formatNum = (v) => {
  const arr = v.toString().split("").reverse();
  let str = "";
  for (let i=arr.length-1;i>=0;i--) {
    if(arr.length == 2 && i==arr.length - 1 && arr[i] == 1) {
      str +=unit[i]
      continue;
    }
    str += (num[arr[i]]+unit[i])
  }
  return str;
};

console.log(formatNum(4515))

 

posted @ 2023-06-14 18:11  abc1234_abc  阅读(73)  评论(0编辑  收藏  举报