js将数字转成中文汉字(仅限1000以内)

let arry = ["零","一","二","三","四","五","六","七","八","九","十"];("零作为补位,避免了数组下标问题")

  transNumToChinese(i) {

    if (i<=10) {

      return this.arry[i];

    }

    if (i%100==0) {

      return this.arry[Math.floor(i/100)] + "百";

    }

    if (i%10==0&&i<100) {

      return this.arry[Math.floor(i/10)] + "十";

    }

    if (i > 10 && i < 20) {

      return "十" + this.transNumToChinese(i-10*Math.floor(i/10));

    }

    if(i>20&&i<100&&i%10!=0) {

      return this.arry[Math.floor(i/10)] + "十" + this.transNumToChinese(i-10*Math.floor(i/10));

    }

    if (i > 100 && i < 1000 && i%100!=0) {

      return this.arry[Math.floor(i/100)] + "百" + this.transNumToChinese(i-100*Math.floor(i/100));

    }

你可能感兴趣的:(js将数字转成中文汉字(仅限1000以内))