转换成金钱格式
export function toMoneyStyle(data: any, step: number, str: string) {
let currency = 'CNY';
let locales = 'zh-CN';
if (Object.prototype.toString.call(data) === '[object String]') {
if (str && str !== '') {
if (str === '$') {
currency = 'USD';
locales = 'en-US';
}
} else {
if (data.includes('$')) {
currency = 'USD';
locales = 'en-US';
}
}
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;
}
}
转换成数字
export function toNumberStyle(data: any) {
let num = data.replace(/[^\d\\.-]/g, '');
return Number(num);
}
转换文件大小
export function convertFileSize(size: number) {
if (size === 0) return '0 B';
let k = 1000;
let sizes: string[] = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
let i = Math.floor(Math.log(size) / Math.log(k));
return (size / Math.pow(k, i)).toPrecision(3) + ' ' + sizes[i];
}