function digitUppercase(n) {
if (!n) {
return '零元整';
}
const fraction = ['角', '分'];
const digit = ['零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖'];
const unit = [
['元', '万', '亿'],
['', '拾', '佰', '仟'],
];
let num = Math.abs(n);
let s = '';
fraction.forEach((item, index) => {
s += (digit[Math.floor(num * 10 * (10 ** index)) % 10] + item).replace(/零./, '');
});
s = s || '整';
num = Math.floor(num);
for (let i = 0; i < unit[0].length && num > 0; i += 1) {
let p = '';
for (let j = 0; j < unit[1].length && num > 0; j += 1) {
p = digit[num % 10] + unit[1][j] + p;
num = Math.floor(num / 10);
}
s = p.replace(/(零.)*零$/, '').replace(/^$/, '零') + unit[0][i] + s;
}
return s.replace(/(零.)*零元/, '元').replace(/(零.)+/g, '零').replace(/^整$/, '零元整');
}
function formatMoney(num, unit = '元') {
if (num === undefined) {
return `0.00${unit}`;
}
if (num === '') {
return;
} else if (isNaN(num)) {
return num;
}
return `${parseFloat(num).toFixed(2).replace(/(\d)(?=(\d{3})+\.)/g, '$1,')}${unit}`;
}
function formatDigitMoney(n) { // 目前只能处理万亿以下的数据
if (!n) {
return '0元';
}
if (n >= 1000000000000) {
return formatMoney(n);
}
const fraction = ['角', '分'];
const digit = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'];
const unit = [
['元', '万', '亿'],
['', '', '', ''],
];
let num = Math.abs(n);
let s = '';
fraction.forEach((item, index) => {
s += (digit[Math.floor(num * 10 * (10 ** index)) % 10] + item).replace(/0./, '');
});
s = s || '';
num = Math.floor(num);
for (let i = 0; i < unit[0].length && num > 0; i += 1) {
let p = '';
for (let j = 0; j < unit[1].length && num > 0; j += 1) {
p = digit[num % 10] + unit[1][j] + p;
num = Math.floor(num / 10);
}
p = `${Math.floor(p)}`;
s = p + unit[0][i] + s;
}
return s.replace(/0000万/, '').replace(/0000/, '').replace(/万0元/, '万元').replace(/亿0万/, '亿')
.replace(/^$/, '0元');
}
function format(value,symbol){ //两个参数,一个是值,一个是货币类型(¥,$)
var obj = {
symbol:symbol||"", //货币类型
int:undefined, //整数位
dec:undefined, //小数位
targ:"", //正负
times:['','万','亿','万亿','亿亿']
}
value = String(value);
var reg = /^-?\d+\.?\d+$/;
if(!reg.test(value)){
alert("请输入数字");
return false;
}
if(value[0]=="-"){
obj.targ = "-";
value = value.substring(1,value.length)
}
var times = 0;
value = Number(value);
while(value >= 10000){
value = value/10000;
times++;
}
value = value.toFixed(2)
var arr = String(value).split(".")
obj.dec = arr[1];
obj.int = arr[0];
if(obj.int.length>3){
obj.int = obj.int.replace(/(.{1})/,'$1,')
}
return obj.symbol+obj.targ+obj.int+"."+obj.dec+obj.times[times];
}
export function formatMoney(value, symbol) {
// 两个参数,一个是值,一个是货币类型(¥,$)
const obj = {
symbol: symbol || '', // 货币类型
int: undefined, // 整数位
dec: undefined, // 小数位
targ: '', // 正负
times: ['', '万', '亿', '万亿', '亿亿']
};
value = String(value);
const reg = /^-?\d+\.?\d+$/;
if (!reg.test(value)) {
return 0;
}
if (value[0] === '-') {
obj.targ = '-';
value = value.substring(1, value.length);
}
let times = 0;
value = Number(value);
while (value >= 10000) {
value /= 10000;
times++;
}
value = value.toFixed(2);
const arr = String(value).split('.');
obj.dec = arr[1];
obj.int = arr[0];
return `${obj.symbol + obj.targ + obj.int}.${obj.dec}<span class="alb-money-unit">${obj.times[times]}</span>`;
}