首页 > 其他分享 >[js] 12位以内的数字转中文

[js] 12位以内的数字转中文

时间:2024-01-19 11:44:50浏览次数:26  
标签:index 中文 12 const temp num js html 64

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta
    name="viewport"
    content="width=device-width, initial-scale=1.0"
  >
  <title>Document</title>
</head>

<body>
  <script>

    function num2Chinese(num) {
      if (num === '0') {
        return '零'
      }
      const numArr = num.toString().replace(/(?=(\d{4})+(?!\d))/g, ',').split(',').filter(Boolean)
      let str = ''
      const binaryUnit = ['', '万', '亿']
      const numUnit = ['', '十', '百', '千']
      const toLower = ['零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖']
      numArr.map((n, idx) => {
        // 去掉0000, 比如说 一亿零万零三百 => 一亿零三百
        if (n !== '0000') {
          let temp = ''
          n.split('').map((ui, uidx) => {
            const uUnit = numUnit[n.length - uidx - 1]
            temp += `${toLower[parseInt(ui)]}${uUnit}`
          })
          // 去掉结尾的零
          str += temp.replace(/(?<=.*?)零+$/g, '')
          const unit = binaryUnit[numArr.length - idx - 1]
          str += unit
        }
      })
      str = str
        .replace(/((零百)|(零千)|(零十))+/g, '零')  // 去掉十百千前面的零
        .replace(/(?<=.*?)零+$/g, '') // 去掉结尾的零
        .replace(/(零+万)/g, '万')  // 去掉万前面的零
        .replace(/(零+亿)/g, '亿') // 去掉亿前面的零
      return str
    }

    // 枚举测试
    const num = ['102000304500', '3', '0', '3004', '30004', '1000000003', '10000200030']

    num.forEach(i => {
      const result = num2Chinese(i)
      console.log(`${i} => ${result}`)
    })

    // 102000304500 => 壹千零贰十亿零叁十万肆千伍百
    // 3 => 叁
    // 3004 => 叁千零肆
    // 30004 => 叁万零肆
    // 1000000003 => 壹十亿零叁
    // 10000200030 => 壹百亿零贰十万零叁十

    for (let i = 0; i <= 100; i++) {
      const result = num2Chinese(i)
      console.log(`${i} => ${result}`)
    }

    // 1 => 壹
    // index.html:64 2 => 贰
    // index.html:64 3 => 叁
    // index.html:64 4 => 肆
    // index.html:64 5 => 伍
    // index.html:64 6 => 陆
    // index.html:64 7 => 柒
    // index.html:64 8 => 捌
    // index.html:64 9 => 玖
    // index.html:64 10 => 壹十
    // index.html:64 11 => 壹十壹
    // index.html:64 12 => 壹十贰
    // index.html:64 13 => 壹十叁
    // index.html:64 14 => 壹十肆
    // index.html:64 15 => 壹十伍
    // index.html:64 16 => 壹十陆
    // index.html:64 17 => 壹十柒
    // index.html:64 18 => 壹十捌
    // index.html:64 19 => 壹十玖
    // index.html:64 20 => 贰十
    // index.html:64 21 => 贰十壹
    // index.html:64 22 => 贰十贰
    // index.html:64 23 => 贰十叁
    // index.html:64 24 => 贰十肆
    // index.html:64 25 => 贰十伍
    // index.html:64 26 => 贰十陆
    // index.html:64 27 => 贰十柒
    // index.html:64 28 => 贰十捌
    // index.html:64 29 => 贰十玖
    // index.html:64 30 => 叁十
    // index.html:64 31 => 叁十壹
    // index.html:64 32 => 叁十贰
    // index.html:64 33 => 叁十叁
    // index.html:64 34 => 叁十肆
    // index.html:64 35 => 叁十伍
    // index.html:64 36 => 叁十陆
    // index.html:64 37 => 叁十柒
    // index.html:64 38 => 叁十捌
    // index.html:64 39 => 叁十玖
    // index.html:64 40 => 肆十
    // index.html:64 41 => 肆十壹
    // index.html:64 42 => 肆十贰
    // index.html:64 43 => 肆十叁
    // index.html:64 44 => 肆十肆
    // index.html:64 45 => 肆十伍
    // index.html:64 46 => 肆十陆
    // index.html:64 47 => 肆十柒
    // index.html:64 48 => 肆十捌
    // index.html:64 49 => 肆十玖
    // index.html:64 50 => 伍十
    // index.html:64 51 => 伍十壹
    // index.html:64 52 => 伍十贰
    // index.html:64 53 => 伍十叁
    // index.html:64 54 => 伍十肆
    // index.html:64 55 => 伍十伍
    // index.html:64 56 => 伍十陆
    // index.html:64 57 => 伍十柒
    // index.html:64 58 => 伍十捌
    // index.html:64 59 => 伍十玖
    // index.html:64 60 => 陆十
    // index.html:64 61 => 陆十壹
    // index.html:64 62 => 陆十贰
    // index.html:64 63 => 陆十叁
    // index.html:64 64 => 陆十肆
    // index.html:64 65 => 陆十伍
    // index.html:64 66 => 陆十陆
    // index.html:64 67 => 陆十柒
    // index.html:64 68 => 陆十捌
    // index.html:64 69 => 陆十玖
    // index.html:64 70 => 柒十
    // index.html:64 71 => 柒十壹
    // index.html:64 72 => 柒十贰
    // index.html:64 73 => 柒十叁
    // index.html:64 74 => 柒十肆
    // index.html:64 75 => 柒十伍
    // index.html:64 76 => 柒十陆
    // index.html:64 77 => 柒十柒
    // index.html:64 78 => 柒十捌
    // index.html:64 79 => 柒十玖
    // index.html:64 80 => 捌十
    // index.html:64 81 => 捌十壹
    // index.html:64 82 => 捌十贰
    // index.html:64 83 => 捌十叁
    // index.html:64 84 => 捌十肆
    // index.html:64 85 => 捌十伍
    // index.html:64 86 => 捌十陆
    // index.html:64 87 => 捌十柒
    // index.html:64 88 => 捌十捌
    // index.html:64 89 => 捌十玖
    // index.html:64 90 => 玖十
    // index.html:64 91 => 玖十壹
    // index.html:64 92 => 玖十贰
    // index.html:64 93 => 玖十叁
    // index.html:64 94 => 玖十肆
    // index.html:64 95 => 玖十伍
    // index.html:64 96 => 玖十陆
    // index.html:64 97 => 玖十柒
    // index.html:64 98 => 玖十捌
    // index.html:64 99 => 玖十玖
    // index.html:64 100 => 壹百
  </script>
</body>

</html>

核心函数

function num2Chinese(num) {
      if (num === '0') {
        return '零'
      }
      const numArr = num.toString().replace(/(?=(\d{4})+(?!\d))/g, ',').split(',').filter(Boolean)
      let str = ''
      const binaryUnit = ['', '万', '亿']
      const numUnit = ['', '十', '百', '千']
      const toLower = ['零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖']
      numArr.map((n, idx) => {
        // 去掉0000, 比如说 一亿零万零三百 => 一亿零三百
        if (n !== '0000') {
          let temp = ''
          n.split('').map((ui, uidx) => {
            const uUnit = numUnit[n.length - uidx - 1]
            temp += `${toLower[parseInt(ui)]}${uUnit}`
          })
          // 去掉结尾的零
          str += temp.replace(/(?<=.*?)零+$/g, '')
          const unit = binaryUnit[numArr.length - idx - 1]
          str += unit
        }
      })
      str = str
        .replace(/((零百)|(零千)|(零十))+/g, '零')  // 去掉十百千前面的零
        .replace(/(?<=.*?)零+$/g, '') // 去掉结尾的零
        .replace(/(零+万)/g, '万')  // 去掉万前面的零
        .replace(/(零+亿)/g, '亿') // 去掉亿前面的零
      return str
    }

 

标签:index,中文,12,const,temp,num,js,html,64
From: https://www.cnblogs.com/fmg0224/p/17974305

相关文章

  • 12星座 十二属相
    十二生肖和十二星座的关系,哪个更准?大美临洮2023-06-2218:03甘肃十二生肖和十二星座都是人类文化传统中非常重要的元素,这两个概念与占星术、命理学等相关联。然而,讨论哪个更准确可能会有一些争议,因为它们各自具有不同的起源和性质。在本文中,我们将探讨什么是十二生肖......
  • 实现modbus plc设备数据转发到环保HJ212平台的方案
    标题:实现modbusplc设备数据转发到环保HJ212平台的方案摘要:通过vfbox网关实现modbus协议转换成HJ212协议,把数据发送到环保平台。此应用方案操作简单,不需要编程,轻松实现设备之间的互联互通。关键词:ModbusHJ212协议转换网关1 需求背景现在大部分省市都建有环保平台用来监控......
  • net8操作appsettings.json类
    1、添回操作类文件AppSettings.csusingMicrosoft.Extensions.Configuration.Json;namespaceYYApi.Helper{///<summary>///appsettings.json操作类///</summary>publicclassAppSettings{publicstaticIConfigurationConfigu......
  • 普普通通入门js案例(原生)
    数组中数据的遍历 vararr=[34,3,4,3]; for(i=0;i<arr.length;i++){ console.log(arr[i]); }求数组中的最大值 vararr=[34,3,4,3]; max=arr[0]; for(i=1;i<arr.length;i++){ if(max<arr[i]){ max=arr[i]; } }求数组中的平......
  • Node.js自建文档(部分)
    目录结构◢modulemoduleA.jsmoduleB.jsindex.js创建package.jsonnpminitindex.js内容constmoduleA={ getName(){ return"moduleAAAAA"; }}改变引入方式(common或module不能混用)添加字段: "type":"module"//es规范导入comment规范引入方式p......
  • datawhale-leetcode打卡:001-012题
    这次这十二个题目属于是极限肝出来的,有两个参考了一下题解,还是很有意思。我会按照我个人的感觉去写这个东西。螺旋矩阵(leetcode054)这个题目比较恶心的就是跑圈的过程怎么描述。首先,顺时针一圈下来是先从左到右,顶到最右边i<m,好再往下,顶到最下边i<n,好现在i--往回排,最后j--走完一......
  • Js(Javascript)的apply call 和bind区别
    ​ apply、call和bind是用于调用函数的三种不同方式,它们的主要区别在于函数调用时的上下文(this关键字)以及参数传递的方式。call和apply是用于立即调用函数并设置this上下文的方法,它们的主要区别在于参数传递的方式。bind不会立即执行函数,而是创建一个新的函数,将this......
  • KY129 简单计算器C++
    、这是目前阶段做的最难最吃力的题目。调试了一遍又一遍去看逻辑上出现的各种问题。。。#include<iostream>#include<string>#include<stack>#include<map>usingnamespacestd;intmain(){map<char,int>m={{'+',0},{'-',0},......
  • P9012 [USACO23JAN] Moo Operations B题解
    第1道赛场AC的题,必须发篇题解记录一下。Tips:\(1\le|S|\le100\)——题目才100,这就可以随便整活了。如果你稍微懂点英语,就会知道第\(2\sim4\)个点的\(S\)都最多只有\(3\)个字符,而目标“MOO”也是\(3\)个字符,所以只需要模拟就可以了。intcheck(string......
  • 912.排序数组--归并排序
    1.题目介绍给你一个整数数组nums,请你将该数组升序排列。示例1:输入:nums=[5,2,3,1]输出:[1,2,3,5]示例2:输入:nums=[5,1,1,2,0,0]输出:[0,0,1,1,2,5]2.题解2.1归并排序思路归并排序利用了分治的思想来对序列进行排序。对一个长为n的待排序的序列,我们将其分解成两个......