小程序增加货币js工具类

This commit is contained in:
aixianling
2022-07-14 14:11:51 +08:00
parent 05c8b55eda
commit 860163bb6c
2 changed files with 22 additions and 1 deletions

View File

@@ -0,0 +1,19 @@
export default {
cny(money) {
if (money) {
money.toLocaleString('zh-Hans-CN', {style: 'currency', currency: "CNY"})
}
return money
},
cn(money) {
let num = parseFloat(money), cnMoney = '',
units = '仟佰拾亿仟佰拾万仟佰拾元角分',
cnNum = '零壹贰叁肆伍陆柒捌玖'
num = num.toFixed(2).replace(/\./g, '')
units = units.substring(units.length - num.length)
Array.from(num).map((e, i) => {
cnMoney += cnNum.charAt(e) + units.charAt(i)
})
return cnMoney.replace(/零角零分$/, '整').replace(/零[仟佰拾]/g, '零').replace(/零{2,}/g, '零').replace(/零([亿|万])/g, '$1').replace(/零+元/, '元').replace(/亿零{0,3}万/, '亿').replace(/^元/, "零元")
}
}

View File

@@ -1,6 +1,7 @@
import $dayjs from './moment'
import $dict from './dict'
import $qs from 'query-string'
import $coin from './coin'
const $toast = (obj) => {
let params = {title: '', duration: 2000, icon: 'none'};
@@ -342,5 +343,6 @@ export default {
$dayjs,
$dict,
$getLoginCode,
$qs
$qs,
$coin
};