import { App } from 'vue'
// ¥6,285
function toIntMark(value) {
try {
if (value.toString().includes('%')) {
return value
}
const newVal = parseFloat(value).toFixed(0)
if (newVal == 'NaN') {
return value
}
return '¥' + newVal.replace(/(\d{1,3})(?=(\d{3})+(?:$|\.))/g, '$1,')
} catch (e) {
return value
}
}
// 6,285
function toInt(value) {
try {
if (value.toString().includes('%')) {
return value
}
const newVal = parseFloat(value).toFixed(0)
if (newVal == 'NaN') {
return value
}
return newVal.replace(/(\d{1,3})(?=(\d{3})+(?:$|\.))/g, '$1,')
} catch (e) {
return value
}
}
// ¥6k
function toThousand(value) {
try {
if (!value) {
return 0
}
if (value.toString().includes('%')) {
return value
}
const newVal = parseFloat(value / 1000 + '').toFixed(0)
if (newVal == 'NaN') {
return value
}
return '¥' + newVal.replace(/(\d{1,3})(?=(\d{3})+(?:$|\.))/g, '$1,') + 'k'
} catch (e) {
return value
}
}
// ¥6,285.00
function toRoundMark(value) {
try {
if (value.toString().includes('%')) {
return value
}
const newVal = parseFloat(value).toFixed(2)
if (newVal == 'NaN') {
return value
}
return '¥' + newVal.replace(/(\d{1,3})(?=(\d{3})+(?:$|\.))/g, '$1,')
} catch (e) {
return value
}
}
// 6285.00
function toRound(value) {
try {
if (value.toString().includes('%')) {
return value
}
const newVal = parseFloat(value).toFixed(2)
if (newVal == 'NaN') {
return value
}
return newVal
} catch (e) {
return value
}
}
// 去掉首尾空格
function Trim(str) {
return str.replace(/(^\s*)|(\s*$)/g, '')
}
export default function (app: App) {
app.config.globalProperties.$toIntMark = toIntMark
app.config.globalProperties.$toInt = toInt
app.config.globalProperties.$toThousand = toThousand
app.config.globalProperties.$toRoundMark = toRoundMark
app.config.globalProperties.$toRound = toRound
}
export { toIntMark, toInt, toThousand, toRoundMark, toRound, Trim }
JS金钱(¥)格式化
于 2022-01-12 10:50:49 首次发布