first
This commit is contained in:
37
commons/utils/cal.js
Normal file
37
commons/utils/cal.js
Normal file
@@ -0,0 +1,37 @@
|
||||
/**
|
||||
* 数字, 计算相关函数
|
||||
*
|
||||
* @author terrfly
|
||||
* @site https://www.jeepay.vip
|
||||
* @date 2022/11/22 10:38
|
||||
*/
|
||||
|
||||
/**
|
||||
* 保留小数n位,不进行四舍五入
|
||||
* num你传递过来的数字,
|
||||
* decimal你保留的几位,默认保留小数后两位
|
||||
*/
|
||||
const formatDecimal = function(num, decimal = 2) {
|
||||
num = num.toString()
|
||||
const index = num.indexOf('.')
|
||||
if (index !== -1) {
|
||||
num = num.substring(0, decimal + index + 1)
|
||||
} else {
|
||||
num = num.substring(0)
|
||||
}
|
||||
//截取后保留两位小数
|
||||
return parseFloat(num).toFixed(decimal)
|
||||
}
|
||||
|
||||
const model = {
|
||||
// 分转元
|
||||
// amount - 金额 parseFloat - 是否转换为数字格式, 默认String
|
||||
cert2Dollar(amount, needParseFloat = false) {
|
||||
if (needParseFloat) { // parseFlot
|
||||
return formatDecimal(amount / 100)
|
||||
}
|
||||
return formatDecimal(amount / 100)
|
||||
}
|
||||
}
|
||||
|
||||
export default model
|
||||
Reference in New Issue
Block a user