因项目需要,在前端页面需要对金额进行规则进行运算,用javascript自带的运算浮点数时会出现精度差异,经各种搜索和实验及整理,特此记录以后备用。
/*
* 由于计算机是用二进制来存储和处理数字,不能精确表示浮点数,而javascript中没有相应的封装类来处理浮点数运算
* 直接计算会导致运算精度丢失。
* 为了避免产生精度差异,把需要计算的数字升级成计算机能够精确识别的整数,等计算完毕再降级,这是大部分编程序
* 语言处理精度差异的通用方法。
*/
/*
* 左补齐字符串
* @param nSize 要补齐的长度
* @param ch 要补齐的字符
*/
String.prototype.padLeft = function(nSize,ch){
var len = 0;
var s = this ? this : "";
ch = ch ? ch : '0';//默认补0
len = s.length;
while(len<nSize){
s = ch + s;
len++;
}
return s;
};
/*
* 右补齐字符串
* @param nSize 要补齐的长度
* @param ch 要补齐的字符
*/
String.prototype.padRight = function(nSize,ch){
var len = 0 ;
var s = this ? this : "";
ch = ch ? ch : '0';//默认补0
len = s.length;
while(len<nSize){
s = s + ch;
len++;
}
return s;
};
/*
* 左移小数点位置 (用于数学计算,相当于除以Math.pow(10,scale)
* @param scale 要移位的刻度
* @return
*/
String.prototype.movePointLeft = function(scale){
var s,s1,s2,ch,ps,sign;
ch = ".";
sign = '';
s = this ? this : "";
if(scale <=0){
return s;
}
ps = s.split('.');
s1 = ps[0] ? ps[0] : "";
s2 = ps[1] ? ps[1] : "";
if(s1.slice(0,1)=='-'){
s1 = s1.slice(1);
sign = '-';
}
if(s1.length<=scale){
ch = "0.";
s1 = s1.padLeft(scale);
}
return sign + s1.slice(0,-scale) + ch + s1.slice(-scale) + s2;
};
/*
* 右移小数点位置 (用于数学计算,相当于除以Math.pow(10,scale)
* @param scale 要移位的刻度
* @return
*/
String.prototype.movePointRight = function(scale){
var s,s1,s2,ch,ps;
ch = '.';
s = this ? this : "";
if(scale<=0){
return s;
}
ps = s.split('.');
s1 = ps[0] ? ps[0] : "";
s2 = ps[1] ? ps[1] : "";
if(s2.length<=scale){
ch = '';
s2 = s2.padRight(scale);
}
return s1 + s2.slice(0,scale) + ch + s2.slice(scale,s2.length);
};
/*
* 移动小数点位置 (用于数学计算,相当于除以Math.pow(10,scale)
* @param scale 要移位的刻度(正数表示向右移,负数表示向左移,0返回返值)
* @return
*/
Strng.prototype.movePoint = function(scale){
if(scale >= 0){
return this.movePointRight(scale);
}else{
return this.movePointLeft(scale);
}
};
/*
* 加法
* @param arg 加数,可以是字符串或数字
*/
Number.prototype.add = function(arg){
var n,n1,n2,s,s1,s2,ps;
s1 = this.toString();
ps = s1.split('.');
n1 = ps[1] ? ps[1].length : 0;
s2 = arg.toString();
ps = s2.split('.');
n2 = ps[1] ? ps[1].length : 0;
n = n1 > n2 ? n1 : n2;
s = Number(s1.movePoint(n)) + Number(s2.movePoint(n));
s = s.toString().movePoint(-n);
return Number(s);
};
/*
* 减法
* @param arg 减数,可以是字符串或数字
*/
Number.prototype.sub = function(arg){
var n,n1,n2,s,s1,s2,ps;
s1 = this.toString();
ps = s1.split('.');
n1 = ps[1] ? ps[1].length : 0;
s2 = arg.toString();
ps = s2.split('.');
n2 = ps[1] ? ps[1].length : 0;
n = n1 > n2 ? n1 : n2;
s = Number(s1.movePoint(n)) - Number(s2.movePoint(n));
s = s.toString().movePoint(-n);
return Number(s);
};
/*
* 乘法
* @param arg 乘数,可以是字符串或数字
*/
Number.prototype.mul = function(arg){
var n,n1,n2,s,s1,s2,ps;
s1 = this.toString();
ps = s1.split('.');
n1 = ps[1] ? ps[1].length : 0;
s2 = arg.toString();
ps = s2.split('.');
n2 = ps[1] ? ps[1].length : 0;
n = n1 + n2;
s = Number(s1.replace('.','')) * Number(s2.replace('.',''));
s = s.toString().movePoint(-n);
return Number(s);
};
/*
* 除法
* @param arg 除数,可以是字符串或数字
*/
Number.prototype.div = function(arg){
var n,n1,n2,s,s1,s2,ps;
s1 = this.toString();
ps = s1.split('.');
n1 = ps[1] ? ps[1].length : 0;
s2 = arg.toString();
ps = s2.split('.');
n2 = ps[1] ? ps[1].length : 0;
n = n1 - n2;
s = Number(s1.replace('.','')) / Number(s2.replace('.',''));
s = s.toString().movePoint(-n);
return Number(s);
};
/*
* 取模
* @param arg 模数,可以是字符串或数字
*/
Number.prototype.mod = function(arg){
var n,n1,n2,s,s1,s2,ps;
s1 = this.toString();
ps = s1.split('.');
n1 = ps[1] ? ps[1].length : 0;
s2 = arg.toString();
ps = s2.split('.');
n2 = ps[1] ? ps[1].length : 0;
n = n1 > n2 ? n1 : n2;
s = Number(s1.movePoint(n)) % Number(s2.movePoint(n));
s = s.toString().movePoint(-n);
return Number(s);
};
本文介绍了一种使用JavaScript处理浮点数运算的方法,通过自定义扩展字符串和数字原型,实现了加、减、乘、除及取模运算的高精度计算。
3424

被折叠的 条评论
为什么被折叠?



