//转换成金钱格式
function toMoneyStyle(data,step,str){
/*
* data 需要格式化的数据 类型是Number/String
* step 保留的小数位数
* str 展示的是美元格式还是人民币 默认人民币格式 参数为 ¥-->人民币 / $--> 美元
*/
if(Object.prototype.toString.call(data) === '[object String]'){
let currency = 'CNY';
let locales = 'zh-CN';
if(str && str != ''){
if(str == '$'){
currency = 'USD';
locales = 'en-US';
}
// else if(str == '¥'){
// currency = 'CNY';
// locales = 'zh-CN';
// }
}else{
if(data.includes('$')){
currency = 'USD';
locales = 'en-US';
}
// else if(data.includes('¥')){
// currency = 'CNY';
// locales = 'zh-CN';
// }
}
let num = data.replace(/[^\d\\.-]/g, '');
if(num != ''){
num = Number(num);
let options = {
style:'currency',
currency:currency,
maximumFractionDigits:step,
useGrouping:true
}
let dataStr = num.toLocaleString(locales,options);
return dataStr
}
}else if(Object.prototype.toString.call(data) === '[object Number]'){
let options = {
style:'currency',
currency:'CNY',
maximumFractionDigits:step,
useGrouping:true
}
let num = data.toLocaleString(locales,options);
return num
}
}
//转换成数字
function toNumberStyle(data){
let num = data.replace(/[^\d\\.-]/g, '');
return Number(num)
}
js 数字与金钱格式相互转换
最新推荐文章于 2023-10-18 09:33:29 发布