97 lines
2.2 KiB
JavaScript
97 lines
2.2 KiB
JavaScript
import Decimal from 'decimal.js'
|
|
|
|
/**
|
|
* 格式化金额格式
|
|
* 返回的是字符串23,245.12保留2位小数
|
|
* @param num
|
|
* @returns {string}
|
|
*/
|
|
export function toMoney(num) {
|
|
num = num.toFixed(2)
|
|
num = parseFloat(num)
|
|
num = num.toLocaleString('zh', {
|
|
minimumFractionDigits: 2,
|
|
useGrouping: true
|
|
})
|
|
return num
|
|
}
|
|
|
|
/**
|
|
* 格式化金额格式
|
|
* 返回的是字符串23,245.12保留2位小数
|
|
* @param num
|
|
* @returns {string}
|
|
*/
|
|
export function toDoller(val) {
|
|
return new Decimal(val).div(100).toNumber();
|
|
}
|
|
|
|
/**
|
|
* 格式化金额格式
|
|
* 返回的是字符串23,245.12保留2位小数
|
|
* @param num
|
|
* @returns {string}
|
|
*/
|
|
export function toCent(val) {
|
|
return new Decimal(val).mul(100).toNumber();
|
|
}
|
|
|
|
export function moneyFormat(val) {
|
|
return toMoney(toDoller(val));
|
|
}
|
|
|
|
/**
|
|
* 日期格式化
|
|
*/
|
|
|
|
/**
|
|
* 日期格式化
|
|
*/
|
|
export function dateFormat(date, format = 'yyyy-MM-dd') {
|
|
if(typeof(date) === 'string'){
|
|
date = date.replace(/\-/g, "/")
|
|
}
|
|
format = format || 'yyyy-MM-dd hh:mm:ss';
|
|
date = new Date(date);
|
|
if (date !== 'Invalid Date') {
|
|
let o = {
|
|
"M+": date.getMonth() + 1, //month
|
|
"d+": date.getDate(), //day
|
|
"h+": date.getHours(), //hour
|
|
"m+": date.getMinutes(), //minute
|
|
"s+": date.getSeconds(), //second
|
|
"q+": Math.floor((date.getMonth() + 3) / 3), //quarter
|
|
"S": date.getMilliseconds() //millisecond
|
|
}
|
|
if (/(y+)/.test(format)) format = format.replace(RegExp.$1,
|
|
(date.getFullYear() + "").substr(4 - RegExp.$1.length));
|
|
for (let k in o)
|
|
if (new RegExp("(" + k + ")").test(format))
|
|
format = format.replace(RegExp.$1,
|
|
RegExp.$1.length === 1 ? o[k] :
|
|
("00" + o[k]).substr(("" + o[k]).length));
|
|
return format;
|
|
}
|
|
return '';
|
|
}
|
|
|
|
export function phoneFilter(val) {
|
|
return val.substring(0, 3) + '****' + val.substring(7)
|
|
}
|
|
|
|
export function idNumberFilter(val) {
|
|
return val.substring(0, 3) + '************' + val.substring(14)
|
|
}
|
|
|
|
export function bankCardFilter(val) {
|
|
return val.substring(0, 4) + ' **** **** ' + val.substring(val.length - 4)
|
|
}
|
|
|
|
export function moneyComdify(val){
|
|
var n = (val/100).toFixed(2);
|
|
var re = /\d{1,3}(?=(\d{3})+$)/g;
|
|
var num = n.replace(/^(\d+)((\.\d+)?)$/,function(s,s1,s2){return s1.replace(re,"$&,")+s2;});
|
|
return num;
|
|
}
|
|
|