mirror of
https://github.com/yoimiya-kokomi/miao-plugin.git
synced 2024-11-25 17:35:34 +00:00
26 lines
747 B
JavaScript
26 lines
747 B
JavaScript
import lodash from 'lodash'
|
|
import Elem from './common/Elem.js'
|
|
import { Cfg } from '#miao'
|
|
|
|
let Format = {
|
|
...Elem,
|
|
int: function (d) {
|
|
return parseInt(d)
|
|
},
|
|
comma: function (num, fix = 0) {
|
|
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')
|
|
integer = integer.replace(re, '$&,') // 正则先行断言 = /\d(?=(\d{3})+$)/g
|
|
return `${integer}${fix > 0 ? '.' + (decimal || lodash.repeat('0', fix)) : ''}`
|
|
},
|
|
pct: function (num, fix = 1) {
|
|
return (num * 1).toFixed(fix) + '%'
|
|
},
|
|
percent: function (num, fix = 1) {
|
|
return Format.pct(num * 100, fix)
|
|
}
|
|
}
|
|
|
|
export default Format
|