megautils-js
Version:
## Project Description
71 lines (62 loc) • 1.53 kB
JavaScript
/**
* @func 金额类工具
*/
const NP = require('number-precision')
module.exports = {
// MARK - Public
// ************************************************************************
/**
* @func 数组转换成显示金额
* @param {Int} num
* @returns
*/
get_currency(num) {
let new_num = 0
if (num > 0) {
// new_num = Math.floor(num * 100) / 100
new_num = NP.divide(Math.floor(NP.times(num, 100)), 100)
} else {
// new_num = - (Math.floor(-num * 100) / 100)
new_num = - NP.divide(Math.floor(NP.times(-num, 100)), 100)
}
return new_num
},
/**
* @func 展示金额小数点2位
* @param {Int} num
*/
display_currency(num) {
return Number.parseFloat(num).toFixed(2)
},
/**
* @func 展示大额金额
* @param {Int} num
*/
display_large_currency(num) {
if (typeof num !== 'number') {
num = parseFloat(num) || 0;
}
return num.toLocaleString(undefined, {
minimumFractionDigits: 2,
maximumFractionDigits: 2
});
},
/**
* 金额 -> int * 100
* @param {*} x
*/
currency_to_int_times_100(x) {
x *= 100
x = Math.round(x)
return x
},
/**
* int -> 金额 / 100
* @param {*} x
*/
currency_int_to_currency_divide_100(x) {
x = Math.round(x)
x = NP.divide(x, 100)
return x
}
}