# javascript中计算超大数据的方法

var num1 = new Number('2222222222222222222222222222222222222222222222222222222222222222222222')
var num2 = new Number('11111111111111111111111111111111111111111111111111111111111111111111111111111111')
var num3 = new Number(num1 + num2).toLocaleString()
console.log(typeof num3 )
console.log( num3.replace(/,/g,'') )

    /**
* 计算大数字，加减
*/
calNum(num1:string , num2:string):string{
if(num1 == '' || num2 == '' || escape(num1).indexOf("%u")!=-1 || escape(num2).indexOf("%u")!=-1){
console.error('num1='+num1+' num2='+num2);
return;
}
let _num1 = new Number(num1);
let _num2 = new Number(num2);
let _num3 = new Number(_num1 + _num2).toLocaleString();
let _num4 = _num3.replace(/,/g,'');
// console.log(typeof num3 )
// console.log( num3.replace(/,/g,'') )
return _num4;
}

/**
* 计算大数字，除法
*/
calSubNum(num1:string , num2:string):string{
if(num1 == '' || num2 == '' || escape(num1).indexOf("%u")!=-1 || escape(num2).indexOf("%u")!=-1){
console.error('num1='+num1+' num2='+num2);
return;
}
let _num1 = new Number(num1);
let _num2 = new Number(num2);
let _num3 = new Number(_num1 / _num2).toLocaleString();
let _num4 = _num3.replace(/,/g,'');
// console.log(typeof num3 )
// console.log( num3.replace(/,/g,'') )
return _num4.split('.')[0];
}

/**
* 计算大数字，乘法
*/
calMulNum(num1:string , num2:string):string{
if(num1 == '' || num2 == '' || escape(num1).indexOf("%u")!=-1 || escape(num2).indexOf("%u")!=-1){
console.error('num1='+num1+' num2='+num2);
return;
}
let _num1 = new Number(num1);
let _num2 = new Number(num2);
let _num3 = new Number(_num1 * _num2).toLocaleString();
let _num4 = _num3.replace(/,/g,'');
// console.log(typeof num3 )
// console.log( num3.replace(/,/g,'') )
return _num4.split('.')[0];
}


©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客