• javascript 实现数字转中文数字[原创]


     1 function numToChinese(num) {
     2         this.chnum = ['零', '一', '二', '三', '四', '五', '六', '七', '八', '九', '十'];
     3         this.chsection = {
     4             unit: {
     5                 2: '十',
     6                 3: '百',
     7                 4: '千',
     8                 5: '万',
     9                 9: '亿',
    10                 13: '兆',
    11                 17: ''
    12             },
    13             next: function (num) {
    14                 for (let i in this.unit) {
    15                     if (i > num) {
    16                         return i;
    17                     }
    18                 }
    19                 return null;
    20             },
    21             get: function (index) {
    22                 let min = 2, q;
    23                 for (let i in this.unit) {
    24                     if (i > index) {
    25                         q = index - min + 1;
    26                         break;
    27                     }
    28                     min = i;
    29                 }
    30                 return this.unit[q];
    31             }
    32         };
    33         const getUnit = (narr, i) => {
    34             const len = narr.length - i;
    35             const num = Number(narr[i]);
    36             if (len < 2) {
    37                 return null;
    38             }
    39             let unit = this.chsection.unit[len];
    40             if (num === 0) {
    41                 if (len < 5 || !unit) {
    42                     return null;
    43                 }
    44                 let isZero = true;
    45                 const next = this.chsection.next(len);
    46                 if (!next) {
    47                     isZero = false;
    48                 } else {
    49                     for (let i = len; i < next; i++) {
    50                         const index = narr.length - i;
    51                         if (narr[index] && narr[index] !== '0') {
    52                             isZero = false;
    53                             break;
    54                         }
    55                     }
    56                 }
    57                 if (isZero) return null;
    58             }
    59             if (unit) {
    60                 return unit;
    61             } else {
    62                 return this.chsection.get(len);
    63             }
    64         }
    65 
    66         num = Number(num);
    67         if (num < 10) {
    68             return this.chnum[num];
    69         }
    70         let chstr = '';
    71         let zero = false;
    72         // let model = 0;
    73         const narr = String(num).split('');
    74         for (let index = 0; index < narr.length; index++) {
    75             const num = Number(narr[index]);
    76             const str = this.chnum[narr[index]];
    77             const unit = getUnit(narr, index);
    78             if (num === 0) {
    79                 zero = true;
    80             }
    81             if (zero && num !== 0) {
    82                 chstr += this.chnum[0];
    83                 zero = false;
    84             }
    85             if (!zero && (unit !== '十' || num !== 1 || index !== 0))
    86                 chstr += str;
    87             chstr += unit || '';
    88         }
    89         return chstr;
    90     }

    使用方法:

    numToChinese(6542127);

  • 相关阅读:
    Android进阶篇判断3G/WIFI/WAP
    Android基础篇配置查看Android系统源码
    Android进阶篇Http协议
    Android进阶篇流量统计
    Android进阶篇自定义Menu(设置Menu的背景及文字属性)
    Android进阶篇Gson解析Json数据
    Android进阶篇PopupWindow的使用
    Android数据篇SAX解析XML
    Android进价篇重力感应
    Android进阶篇ListView和Button共存
  • 原文地址:https://www.cnblogs.com/Red-ButterFly/p/10396747.html
Copyright © 2020-2023  润新知