miao-plugin/components/Format.js

26 lines
747 B
JavaScript
Raw Normal View History

import lodash from 'lodash'
2023-02-13 20:43:14 +00:00
import Elem from './common/Elem.js'
2023-03-07 17:52:11 +00:00
import { Cfg } from '#miao'
2022-04-14 20:53:22 +00:00
let Format = {
2022-11-24 15:27:03 +00:00
...Elem,
2022-04-14 20:53:22 +00:00
int: function (d) {
2022-07-27 18:36:49 +00:00
return parseInt(d)
2022-04-14 20:53:22 +00:00
},
comma: function (num, fix = 0) {
2022-07-27 18:36:49 +00:00
num = parseFloat((num * 1).toFixed(fix))
let [integer, decimal] = String.prototype.split.call(num, '.')
let re = new RegExp(`\\d(?=(\\d{${Cfg.get('commaGroup', 3)}})+$)`, 'g')
2022-11-24 10:30:56 +00:00
integer = integer.replace(re, '$&,') // 正则先行断言 = /\d(?=(\d{3})+$)/g
return `${integer}${fix > 0 ? '.' + (decimal || lodash.repeat('0', fix)) : ''}`
2022-04-14 20:53:22 +00:00
},
pct: function (num, fix = 1) {
2022-07-27 18:36:49 +00:00
return (num * 1).toFixed(fix) + '%'
2022-04-14 20:53:22 +00:00
},
percent: function (num, fix = 1) {
2022-07-27 18:36:49 +00:00
return Format.pct(num * 100, fix)
2022-04-14 20:53:22 +00:00
}
}
2022-07-27 18:36:49 +00:00
export default Format