瀏覽代碼

金额转换成英文写法

lxf 2 年之前
父節點
當前提交
d4b9c9aace
共有 3 個文件被更改,包括 144 次插入3 次删除
  1. 2 1
      src/main.js
  2. 113 0
      src/utils/ACapital.js
  3. 29 2
      src/utils/util.js

+ 2 - 1
src/main.js

@@ -44,7 +44,7 @@ import {
   selectDictLabels
 } from '@/utils/ruoyi'
 
-import { dictDataEcho, dictValueLabel, moneyFormat, calculationWeek, getDict, getPdf } from '@/utils/util'
+import { dictDataEcho, dictValueLabel, moneyFormat, calculationWeek, getDict, getPdf, translateIntoEnglish } from '@/utils/util'
 
 // 分页组件
 import Pagination from '@/components/Pagination'
@@ -81,6 +81,7 @@ app.config.globalProperties.moneyFormat = moneyFormat
 app.config.globalProperties.calculationWeek = calculationWeek
 app.config.globalProperties.getDict = getDict
 app.config.globalProperties.getPdf = getPdf
+app.config.globalProperties.translateIntoEnglish = translateIntoEnglish
 
 
 // 全局组件挂载

+ 113 - 0
src/utils/ACapital.js

@@ -0,0 +1,113 @@
+;(function (s) {
+  //参数
+  var NumtoEnglish = {},
+    n = '',
+    xiao = '',
+    zheng = '',
+    regxinteger = /^([0-9]{1,}([.][0-9]*)?)$/
+  //数字英文写法
+  NumtoEnglish.tally = {
+    arr1: ['zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine'],
+    arr2: ['ten', 'eleven', 'twelve', 'thirteen', 'fourteen', 'fifteen', 'sixteen', 'seventeen', 'eighteen', 'nineteen'],
+    arr3: ['twenty', 'thirty', 'forty', 'fifty', 'sixty', 'seventy', 'eighty', 'ninety'],
+    arr4: ['hundred', 'thousand', 'million', 'billion', 'trillion', 'quadrillion'],
+  }
+  //转换整数部分
+  NumtoEnglish.Convert_integer = function (n) {
+    try {
+      var fenge = this.toThousands(n).split(',')
+      var result = ''
+      for (var i = 0; i < fenge.length; i++) {
+        if (fenge[i].length == 3) {
+          result += this.tally.arr1[fenge[i].substring(0, 1)] + ' ' //百位
+          result += this.tally.arr4[0]
+          if (this.doubledight(fenge[i].substring(1)) != '') {
+            result += ' and ' + this.doubledight(fenge[i].substring(1))
+          }
+        } else if (fenge[i].length == 2) {
+          result += this.doubledight(fenge[i]) //十位
+        } else if (fenge[i].length == 1) {
+          result += this.tally.arr1[fenge[i]] //个位
+        }
+        //添加千分位单位(数字超过1000,每三位数字分配一个单位)
+        if (i < fenge.length - 1) {
+          result += ' ' + this.tally.arr4[fenge.length - 1 - i] + ' '
+        }
+      }
+      return result
+    } catch (ex) {
+      console.error(ex)
+    }
+  }
+  //转换小数部分
+  NumtoEnglish.Convert_decimal = function (n) {
+    var d = n.split('')
+    var result = ''
+    if (d.length > 0) {
+      for (let i = 0; i < d.length; i++) {
+        result += this.Convert_integer(d[i]) + ' '
+      }
+
+      //d.forEach(a => {
+      //   result += this.Convert_integer(a) + " ";
+      //});
+    }
+    return result
+  }
+  //组合两位数
+  NumtoEnglish.doubledight = function (n) {
+    var result = ''
+    if (parseInt(n) != 0) {
+      var dd = n.split('')
+      if (dd[0] < 1) {
+        result = this.tally.arr1[dd[1]]
+      } else if (dd[0] == 1) {
+        result = this.tally.arr2[dd[1]]
+      } else {
+        if (this.tally.arr1[dd[1]] !== 'zero') {
+          result = this.tally.arr3[dd[0] - 2] + '-' + this.tally.arr1[dd[1]]
+        } else {
+          result = this.tally.arr3[dd[0] - 2]
+        }
+      }
+    }
+    return result
+  }
+
+  //转换千分位显示,例:1000000 = 1,000,000
+  NumtoEnglish.toThousands = function (num) {
+    var num = (num || 0).toString(),
+      result = ''
+    while (num.length > 3) {
+      result = ',' + num.slice(-3) + result
+      num = num.slice(0, num.length - 3)
+    }
+    if (num) {
+      result = num + result
+    }
+    return result
+  }
+
+  //扩展String方法
+  s.prototype.toEnglish = function () {
+    n = this
+    if (!regxinteger.test(parseInt(n))) {
+      return 'Error:Must in digital format'
+    }
+
+    //分割整数和小数(如果有小数的话)
+    var NumList = n.toString().split('.'),
+      zheng = NumtoEnglish.Convert_integer(NumList[0]) //整数部分
+    //如果分割长度是2,说明是小数
+    if (NumList.length == 2) {
+      if (NumList[1].length <= 2) {
+        xiao = NumtoEnglish.Convert_decimal(NumList[1])
+      } else {
+        //如果小数超过2位,不转换,返回原数据
+        return n
+      }
+    }
+    //返回转换结果
+    return zheng + (xiao == '' ? '' : ' point ' + xiao)
+  }
+})(String)

+ 29 - 2
src/utils/util.js

@@ -1,8 +1,9 @@
 import moment from "moment";
 import { post, get } from "@/utils/request";
 import Cookies from "js-cookie";
-import html2canvas from 'html2canvas'
-import JsPDF from 'jspdf'
+import html2canvas from "html2canvas";
+import JsPDF from "jspdf";
+import * as toEnglish from "./ACapital.js";
 
 //根据value值回显字典label值
 export function dictDataEcho(value, arr) {
@@ -192,3 +193,29 @@ export function getPdf(title) {
     });
   }, 1000);
 }
+
+export function currencyPrefix(key) {
+  if (["¥", "¥", "1"].includes(key)) {
+    return "SAY RMB ";
+  } else if (["$", "2"].includes(key)) {
+    return "SAY US DOLLARS ";
+  } else if (["€", "3"].includes(key)) {
+    return "SAY EURO ";
+  } else {
+    return "SAY RMB ";
+  }
+}
+
+export function translateIntoEnglish(money, currencyType) {
+  let text = "";
+  if (money) {
+    text = currencyPrefix(currencyType);
+    if (!/^\d+$/.test(Number(money))) {
+      text = text + parseFloat(money).toFixed(2).toEnglish().toUpperCase();
+    } else {
+      text = text + parseFloat(money).toFixed(2).toEnglish().toUpperCase() + " ONLY";
+    }
+  }
+  text = text.split(" POINT ZERO ZERO ").join("");
+  return text;
+}