JavaScript实现把数字转换成中文

作者:junjie 时间:2024-04-30 10:08:40 


var _change = {
           ary0:["零", "一", "二", "三", "四", "五", "六", "七", "八", "九"],
           ary1:["", "十", "百", "千"],
           ary2:["", "万", "亿", "兆"],
           init:function (name) {
               this.name = name;
           },
           strrev:function () {
               var ary = []
               for (var i = this.name.length; i >= 0; i--) {
                   ary.push(this.name[i])
               }
               return ary.join("");
           }, //倒转字符串。
           pri_ary:function () {
               var $this = this
               var ary = this.strrev();
               var zero = ""
               var newary = ""
               var i4 = -1
               for (var i = 0; i < ary.length; i++) {
                   if (i % 4 == 0) { //首先判断万级单位,每隔四个字符就让万级单位数组索引号递增
                       i4++;
                       newary = this.ary2[i4] + newary; //将万级单位存入该字符的读法中去,它肯定是放在当前字符读法的末尾,所以首先将它叠加入$r中,
                       zero = ""; //在万级单位位置的“0”肯定是不用的读的,所以设置零的读法为空
 
                   }
                   //关于0的处理与判断。
                   if (ary[i] == '0') { //如果读出的字符是“0”,执行如下判断这个“0”是否读作“零”
                       switch (i % 4) {
                           case 0:
                               break;
                           //如果位置索引能被4整除,表示它所处位置是万级单位位置,这个位置的0的读法在前面就已经设置好了,所以这里直接跳过
                           case 1:
                           case 2:
                           case 3:
                               if (ary[i - 1] != '0') {
                                   zero = "零"
                               }
                               ; //如果不被4整除,那么都执行这段判断代码:如果它的下一位数字(针对当前字符串来说是上一个字符,因为之前执行了反转)也是0,那么跳过,否则读作“零”
                               break;
 
                       }
 
                       newary = zero + newary;
                       zero = '';
                   }
                   else { //如果不是“0”
                       newary = this.ary0[parseInt(ary[i])] + this.ary1[i % 4] + newary; //就将该当字符转换成数值型,并作为数组ary0的索引号,以得到与之对应的中文读法,其后再跟上它的的一级单位(空、十、百还是千)最后再加上前面已存入的读法内容。
                   }
 
               }
               if (newary.indexOf("零") == 0) {
                   newary = newary.substr(1)
               }//处理前面的0
               return newary;
           }
       }
 
       //创建class类
       function change() {
           this.init.apply(this, arguments);
       }
       change.prototype = _change
 
//创建实例
       var k = new change("00102040");
       alert(k.pri_ary())
标签:JavaScript,数字,转换,中文
0
投稿

猜你喜欢

  • 组件:Adodb.Stream 用法介绍

    2008-10-09 12:39:00
  • Python获取运行目录与当前脚本目录的方法

    2023-12-19 13:17:59
  • Golang使用Gin创建Restful API的实现

    2024-05-21 10:24:04
  • python实现简单的五子棋游戏

    2023-07-30 13:24:31
  • PHP实现的线索二叉树及二叉树遍历方法详解

    2023-11-13 11:28:06
  • python操作excel之xlwt与xlrd

    2023-02-11 08:05:52
  • Python实现解析命令行参数的常见方法总结

    2023-10-15 12:31:49
  • Python获取秒级时间戳与毫秒级时间戳的示例代码

    2022-05-05 22:13:00
  • ORACLE常见错误代码的分析与解决三

    2010-07-26 13:28:00
  • python删除列表元素的三种方法(remove,pop,del)

    2022-10-16 11:43:21
  • mysql存储过程原理与用法详解

    2024-01-26 02:50:54
  • JS实现网页滚动条感应鼠标变色的方法

    2024-04-18 10:52:44
  • Python 加密与解密小结

    2021-04-28 00:35:47
  • 利用Python爬取微博数据生成词云图片实例代码

    2023-03-20 15:49:59
  • pip install命令安装扩展库整理

    2021-10-13 21:49:14
  • Python 实现Windows开机运行某软件的方法

    2021-05-15 11:48:42
  • SQLServe 重复行删除方法

    2024-01-26 18:39:24
  • pyramid配置session的方法教程

    2021-04-26 09:23:37
  • python opencv根据颜色进行目标检测的方法示例

    2021-09-29 03:53:41
  • 验证sql保留字工具

    2008-05-15 12:34:00
  • asp之家 网络编程 m.aspxhome.com