update: 工作台金额格式化

This commit is contained in:
admin 2024-04-15 10:54:05 +08:00
parent de79e330eb
commit b1534cf31e
2 changed files with 41 additions and 1 deletions

View File

@ -20,7 +20,7 @@
:src="`https://backend-common.obs.cn-east-3.myhuaweicloud.com/static/pages/${item.imgUrl}`"
/>
<view class="box">
<view class="num">{{ item.num }}</view>
<view class="num">{{ formatMoney(item.num) }}</view>
<view class="title">{{ item.title }}</view>
</view>
</view>
@ -158,6 +158,7 @@
import { useMemberStore } from "@/store/index";
import { ProfileApi, ReceiveApi, ShipmentApi } from "@/services/index";
import TabBar from "@/components/TabBar/index.vue";
import { formatMoney } from "@/utils";
const store = useMemberStore();
const navbarRect = reactive({
height: 32,

View File

@ -160,3 +160,42 @@ export function timeRange(id: number) {
}
return { startTime: startTime, endTime: endTime };
}
// 格式化金额
export function formatMoney(number:any, decimals = 0, decPoint = '.', thousandsSep = ',') {
number = (number + '').replace(/[^0-9+-Ee.]/g, '')
const n = !isFinite(+number) ? 0 : +number
const prec = !isFinite(+decimals) ? 0 : Math.abs(decimals)
const sep = typeof thousandsSep === 'undefined' ? ',' : thousandsSep
const dec = typeof decPoint === 'undefined' ? '.' : decPoint
let s:any = ''
const toFixedFix = function(n:number, prec:number) {
const k = Math.pow(10, prec)
return '' + Math.ceil(signFigures(n * k)) / k
}
s = (prec ? toFixedFix(n, prec) : '' + Math.round(n)).split('.')
const re = /(-?\d+)(\d{3})/
while (re.test(s[0])) {
s[0] = s[0].replace(re, '$1' + sep + '$2')
}
if ((s[1] || '').length < prec) {
s[1] = s[1] || ''
s[1] += new Array(prec - s[1].length + 1).join('0')
}
return s.join(dec)
}
export function signFigures (num:number, rank = 6) {
if (!num) return (0)
const sign = num / Math.abs(num)
const number = num * sign
const temp = rank - 1 - Math.floor(Math.log10(number))
let ans
if (temp > 0) {
ans = parseFloat(number.toFixed(temp))
} else if (temp < 0) {
ans = Math.round(number / Math.pow(10, temp)) * temp
} else {
ans = Math.round(number)
}
return (ans * sign)
}