miao-plugin/components/Format.js

20 lines
533 B
JavaScript
Raw Normal View History

2022-04-14 20:53:22 +00:00
let Format = {
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, '.')
integer = integer.replace(/\d(?=(\d{3})+$)/g, '$&,') // 正则先行断言
return `${integer}${decimal ? '.' + decimal : ''}`
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