JS将阿拉伯数字翻译成中文的大写数字、JS将数字转换为大写金额(整理)

简介: JS将阿拉伯数字翻译成中文的大写数字、JS将数字转换为大写金额(整理)

1、JS将阿拉伯数字翻译成中文的大写数字

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8">
    <meta name="viewport"
      content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no" />
    <title>JS将阿拉伯数字翻译成中文的大写数字</title>
  </head>
  <body>
    JS将阿拉伯数字翻译成中文的大写数字
  </body>
  <script src="https://cdn.bootcss.com/jquery/3.2.1/jquery.js"></script>
  <script type="text/javascript">
    var num = 123.456;
    var AA = new Array("零", "一", "二", "三", "四", "五", "六", "七", "八", "九", "十");
    var BB = new Array("", "十", "百", "仟", "萬", "億", "点", "");
    var a = ("" + num).replace(/(^0*)/g, "").split("."),
      k = 0,
      figure = "";
    for (var i = a[0].length - 1; i >= 0; i--) {
      switch (k) {
        case 0:
          figure = BB[7] + figure;
          break;
        case 4:
          if (!new RegExp("0{4}//d{" + (a[0].length - i - 1) + "}$")
            .test(a[0]))
            figure = BB[4] + figure;
          break;
        case 8:
          figure = BB[5] + figure;
          BB[7] = BB[5];
          k = 0;
          break;
      }
      if (k % 4 == 2 && a[0].charAt(i + 2) != 0 && a[0].charAt(i + 1) == 0)
        figure = AA[0] + figure;
      if (a[0].charAt(i) != 0)
        figure = AA[a[0].charAt(i)] + BB[k % 4] + figure;
      k++;
    }
    if (a.length > 1) // 加上小数部分(如果有小数部分)
    {
      figure += BB[6];
      for (var i = 0; i < a[1].length; i++)
        figure += AA[a[1].charAt(i)];
    }
    if (figure == '一十')
      figure = "十";
    if (figure.match(/^一/) && figure.length == 3)
      figure = figure.replace("一", "");
    // return figure;
    console.log(figure);
  </script>
</html>

2、JS将数字转换为大写金额

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8">
    <meta name="viewport"
      content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no" />
    <title>JS将数字转换为大写金额</title>
  </head>
  <body>
    JS将数字转换为大写金额
  </body>
  <script src="https://cdn.bootcss.com/jquery/3.2.1/jquery.js"></script>
  <script type="text/javascript">
    var Num = 123.45;
    //判断如果传递进来的不是字符的话转换为字符
    if (typeof Num == "number") {
      Num = new String(Num);
    };
    Num = Num.replace(/,/g, "") //替换tomoney()中的“,”
    Num = Num.replace(/ /g, "") //替换tomoney()中的空格
    Num = Num.replace(/¥/g, "") //替换掉可能出现的¥字符
    if (isNaN(Num)) { //验证输入的字符是否为数字
      //alert("请检查小写金额是否正确");
      // return "";
    };
    //字符处理完毕后开始转换,采用前后两部分分别转换
    var part = String(Num).split(".");
    var newchar = "";
    //小数点前进行转化
    for (var i = part[0].length - 1; i >= 0; i--) {
      if (part[0].length > 10) {
        // return "";
        //若数量超过拾亿单位,提示
      }
      var convert = ""
      var perchar = part[0].charAt(i);
      switch (perchar) {
        case "0":
          convert = "零" + convert;
          break;
        case "1":
          convert = "壹" + convert;
          break;
        case "2":
          convert = "贰" + convert;
          break;
        case "3":
          convert = "叁" + convert;
          break;
        case "4":
          convert = "肆" + convert;
          break;
        case "5":
          convert = "伍" + convert;
          break;
        case "6":
          convert = "陆" + convert;
          break;
        case "7":
          convert = "柒" + convert;
          break;
        case "8":
          convert = "捌" + convert;
          break;
        case "9":
          convert = "玖" + convert;
          break;
      }
      switch (part[0].length - i - 1) {
        case 0:
          convert = convert + "元";
          break;
        case 1:
          if (perchar != 0) convert = convert + "拾";
          break;
        case 2:
          if (perchar != 0) convert = convert + "佰";
          break;
        case 3:
          if (perchar != 0) convert = convert + "仟";
          break;
        case 4:
          convert = convert + "万";
          break;
        case 5:
          if (perchar != 0) convert = convert + "拾";
          break;
        case 6:
          if (perchar != 0) convert = convert + "佰";
          break;
        case 7:
          if (perchar != 0) convert = convert + "仟";
          break;
        case 8:
          convert = convert + "亿";
          break;
        case 9:
          convert = convert + "拾";
          break;
      }
      var newchar = convert + newchar;
    }
    //小数点之后进行转化
    if (Num.indexOf(".") != -1) {
      if (part[1].length > 2) {
        // alert("小数点之后只能保留两位,系统将自动截断");
        part[1] = part[1].substr(0, 2)
      }
      for (i = 0; i < part[1].length; i++) {
        convert = ""
        perchar = part[1].charAt(i)
        switch (perchar) {
          case "0":
            convert = "零" + convert;
            break;
          case "1":
            convert = "壹" + convert;
            break;
          case "2":
            convert = "贰" + convert;
            break;
          case "3":
            convert = "叁" + convert;
            break;
          case "4":
            convert = "肆" + convert;
            break;
          case "5":
            convert = "伍" + convert;
            break;
          case "6":
            convert = "陆" + convert;
            break;
          case "7":
            convert = "柒" + convert;
            break;
          case "8":
            convert = "捌" + convert;
            break;
          case "9":
            convert = "玖" + convert;
            break;
        }
        if (i == 0) convert = convert + "角";
        if (i == 1) convert = convert + "分";
        newchar = newchar + convert;
      }
    }
    //替换所有无用汉字
    while (newchar.search("零零") != -1)
      newchar = newchar.replace("零零", "零");
    newchar = newchar.replace("零亿", "亿");
    newchar = newchar.replace("亿万", "亿");
    newchar = newchar.replace("零万", "万");
    newchar = newchar.replace("零元", "元");
    newchar = newchar.replace("零角", "");
    newchar = newchar.replace("零分", "");
    if (newchar.charAt(newchar.length - 1) == "元") {
      newchar = newchar + "整"
    }
    // return newchar;
    console.log(newchar);
  </script>
</html>
阿拉伯数字转中文-[转载-原文地址](https://blog.csdn.net/weixin_48618258/article/details/126846518)
intToChinese(str) {
  console.log(str, 'str')
  str = str + '';
  let len = str.length - 1;
  var idxs = ['', '十', '百', '千', '万', '十', '百', '千', '亿', '十', '百', '千', '万', '十', '百', '千', '亿'];
  var num = ['零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖'];
  return str.replace(/([1-9]|0+)/g, function($, $1, idx, full) {
    var pos = 0;
    if ($1[0] != '0') {
      pos = len - idx;
      if (idx == 0 && $1[0] == 1 && idxs[len - idx] == '十') {
        return idxs[len - idx];
      }
      return num[$1[0]] + idxs[len - idx];
    } else {
      var left = len - idx;
      var right = len - idx + $1.length;
      if (Math.floor(right / 4) - Math.floor(left / 4) > 0) {
        pos = left - left % 4;
      }
      if (pos) {
        return idxs[pos] + num[$1[0]];
      } else if (idx + $1.length >= len) {
        return '';
      } else {
        return num[$1[0]]
      }
    }
  });
},
相关文章
|
8月前
|
JavaScript
JS自动生成速记符、拼音简写/拼音的声母(例如:“你挚爱的强哥”转换为“NZADQG”)。提取首字母,返回大写形式;提取拼音, 返回首字母大写形式(全拼)。
JS自动生成速记符、拼音简写/拼音的声母(例如:“你挚爱的强哥”转换为“NZADQG”)。提取首字母,返回大写形式;提取拼音, 返回首字母大写形式(全拼)。
|
5月前
|
JavaScript
js金额数字格式化
本文介绍了两种实现数字格式化的方法:字符串操作与正则表达式。首先,通过递归地在每三位数字间插入逗号来格式化整数。接着,扩展该功能以支持自定义小数位数及分隔符,适用于更广泛的场景。最后,展示了如何利用正则表达式简化上述过程,实现相同的功能。
102 0
|
6月前
|
JavaScript
js函数封装 —— 金额添加千分位分隔符
js函数封装 —— 金额添加千分位分隔符
77 2
|
6月前
|
JavaScript
js 金额格式化——显示千位分隔符
js 金额格式化——显示千位分隔符
79 0
|
8月前
|
JavaScript 测试技术
【sgGoogleTranslate】自定义组件:基于Vue.js用谷歌Google Translate翻译插件实现网站多国语言开发
【sgGoogleTranslate】自定义组件:基于Vue.js用谷歌Google Translate翻译插件实现网站多国语言开发
|
8月前
|
JavaScript
js中把数字转换成汉字输出
js中把数字转换成汉字输出
180 1
|
JavaScript
JS字符串中的第一个字母大写(两种方法)
JS字符串中的第一个字母大写(两种方法)
102 0
|
JavaScript
js金额补充--2位小数0demo效果(整理)
js金额补充--2位小数0demo效果(整理)
|
算法 JavaScript
W3Cschool编程实战JS脚本算法挑战:设置首字母大写算法挑战
返回一个字符串,确保字符串的每个单词首字母都大写,其余部分小写。
91 0
|
设计模式 JavaScript 前端开发