Browse Source

Merge pull request #505 in VISUAL/fineui from ~YOUNG/fineui:master to master

* commit '85b7bfd85d9f720e8e7d1cbf57d7adfa9e9e7691':
  BI-26291 除法丢精度
master
windy 7 years ago
parent
commit
556d02d066
  1. 70
      dist/bundle.js
  2. 70
      dist/core.js
  3. 70
      dist/fineui.js
  4. 70
      src/core/proto/number.js
  5. 70
      utils/utils.js

70
dist/bundle.js vendored

@ -29973,32 +29973,66 @@ function accMul (arg1, arg2) {
Number.prototype.mul = function (arg) { Number.prototype.mul = function (arg) {
return accMul(arg, this); return accMul(arg, this);
}; };
/** /**
** 除法函数用来得到精确的除法结果 * Return digits length of a number
** 说明javascript的除法结果会有误差在两个浮点数相除的时候会比较明显这个函数返回较为精确的除法结果 * @param {*number} num Input number
** 调用accDiv(arg1,arg2) */
** 返回值arg1除以arg2的精确结果 function digitLength (num) {
**/ // Get digit length of e
function accDiv (arg1, arg2) { var eSplit = num.toString().split(/[eE]/);
var t1 = 0, t2 = 0, r1, r2; var len = (eSplit[0].split(".")[1] || "").length - (+(eSplit[1] || 0));
try { return len > 0 ? len : 0;
t1 = arg1.toString().split(".")[1].length; }
} catch (e) { /**
* 把小数转成整数支持科学计数法如果是小数则放大成整数
* @param {*number} num 输入数
*/
function float2Fixed (num) {
if (num.toString().indexOf("e") === -1) {
return Number(num.toString().replace(".", ""));
} }
try { var dLen = digitLength(num);
t2 = arg2.toString().split(".")[1].length; return dLen > 0 ? num * Math.pow(10, dLen) : num;
} catch (e) { }
/**
* 精确乘法
*/
function times (num1, num2) {
var others = [];
for (var _i = 2; _i < arguments.length; _i++) {
others[_i - 2] = arguments[_i];
}
if (others.length > 0) {
return times.apply(void 0, [times(num1, num2), others[0]].concat(others.slice(1)));
}
var num1Changed = float2Fixed(num1);
var num2Changed = float2Fixed(num2);
var baseNum = digitLength(num1) + digitLength(num2);
var leftValue = num1Changed * num2Changed;
return leftValue / Math.pow(10, baseNum);
}
/**
* 精确除法
*/
function accDivide (num1, num2) {
var others = [];
for (var _i = 2; _i < arguments.length; _i++) {
others[_i - 2] = arguments[_i];
} }
with (Math) { if (others.length > 0) {
r1 = Number(arg1.toString().replace(".", "")); return accDivide.apply(void 0, [accDivide(num1, num2), others[0]].concat(others.slice(1)));
r2 = Number(arg2.toString().replace(".", ""));
return (t2 > t1) ? (r1 / r2) * pow(10, t2 - t1) : (r1 / r2) / pow(10, t1 - t2);
} }
var num1Changed = float2Fixed(num1);
var num2Changed = float2Fixed(num2);
return times((num1Changed / num2Changed), Math.pow(10, digitLength(num2) - digitLength(num1)));
} }
// 给Number类型增加一个div方法,调用起来更加方便。 // 给Number类型增加一个div方法,调用起来更加方便。
Number.prototype.div = function (arg) { Number.prototype.div = function (arg) {
return accDiv(this, arg); return accDivide(this, arg);
};/** };/**
* 对字符串对象的扩展 * 对字符串对象的扩展
* @class String * @class String

70
dist/core.js vendored

@ -29973,32 +29973,66 @@ function accMul (arg1, arg2) {
Number.prototype.mul = function (arg) { Number.prototype.mul = function (arg) {
return accMul(arg, this); return accMul(arg, this);
}; };
/** /**
** 除法函数用来得到精确的除法结果 * Return digits length of a number
** 说明javascript的除法结果会有误差在两个浮点数相除的时候会比较明显这个函数返回较为精确的除法结果 * @param {*number} num Input number
** 调用accDiv(arg1,arg2) */
** 返回值arg1除以arg2的精确结果 function digitLength (num) {
**/ // Get digit length of e
function accDiv (arg1, arg2) { var eSplit = num.toString().split(/[eE]/);
var t1 = 0, t2 = 0, r1, r2; var len = (eSplit[0].split(".")[1] || "").length - (+(eSplit[1] || 0));
try { return len > 0 ? len : 0;
t1 = arg1.toString().split(".")[1].length; }
} catch (e) { /**
* 把小数转成整数支持科学计数法如果是小数则放大成整数
* @param {*number} num 输入数
*/
function float2Fixed (num) {
if (num.toString().indexOf("e") === -1) {
return Number(num.toString().replace(".", ""));
} }
try { var dLen = digitLength(num);
t2 = arg2.toString().split(".")[1].length; return dLen > 0 ? num * Math.pow(10, dLen) : num;
} catch (e) { }
/**
* 精确乘法
*/
function times (num1, num2) {
var others = [];
for (var _i = 2; _i < arguments.length; _i++) {
others[_i - 2] = arguments[_i];
}
if (others.length > 0) {
return times.apply(void 0, [times(num1, num2), others[0]].concat(others.slice(1)));
}
var num1Changed = float2Fixed(num1);
var num2Changed = float2Fixed(num2);
var baseNum = digitLength(num1) + digitLength(num2);
var leftValue = num1Changed * num2Changed;
return leftValue / Math.pow(10, baseNum);
}
/**
* 精确除法
*/
function accDivide (num1, num2) {
var others = [];
for (var _i = 2; _i < arguments.length; _i++) {
others[_i - 2] = arguments[_i];
} }
with (Math) { if (others.length > 0) {
r1 = Number(arg1.toString().replace(".", "")); return accDivide.apply(void 0, [accDivide(num1, num2), others[0]].concat(others.slice(1)));
r2 = Number(arg2.toString().replace(".", ""));
return (t2 > t1) ? (r1 / r2) * pow(10, t2 - t1) : (r1 / r2) / pow(10, t1 - t2);
} }
var num1Changed = float2Fixed(num1);
var num2Changed = float2Fixed(num2);
return times((num1Changed / num2Changed), Math.pow(10, digitLength(num2) - digitLength(num1)));
} }
// 给Number类型增加一个div方法,调用起来更加方便。 // 给Number类型增加一个div方法,调用起来更加方便。
Number.prototype.div = function (arg) { Number.prototype.div = function (arg) {
return accDiv(this, arg); return accDivide(this, arg);
};/** };/**
* 对字符串对象的扩展 * 对字符串对象的扩展
* @class String * @class String

70
dist/fineui.js vendored

@ -30222,32 +30222,66 @@ function accMul (arg1, arg2) {
Number.prototype.mul = function (arg) { Number.prototype.mul = function (arg) {
return accMul(arg, this); return accMul(arg, this);
}; };
/** /**
** 除法函数用来得到精确的除法结果 * Return digits length of a number
** 说明javascript的除法结果会有误差在两个浮点数相除的时候会比较明显这个函数返回较为精确的除法结果 * @param {*number} num Input number
** 调用accDiv(arg1,arg2) */
** 返回值arg1除以arg2的精确结果 function digitLength (num) {
**/ // Get digit length of e
function accDiv (arg1, arg2) { var eSplit = num.toString().split(/[eE]/);
var t1 = 0, t2 = 0, r1, r2; var len = (eSplit[0].split(".")[1] || "").length - (+(eSplit[1] || 0));
try { return len > 0 ? len : 0;
t1 = arg1.toString().split(".")[1].length; }
} catch (e) { /**
* 把小数转成整数支持科学计数法如果是小数则放大成整数
* @param {*number} num 输入数
*/
function float2Fixed (num) {
if (num.toString().indexOf("e") === -1) {
return Number(num.toString().replace(".", ""));
} }
try { var dLen = digitLength(num);
t2 = arg2.toString().split(".")[1].length; return dLen > 0 ? num * Math.pow(10, dLen) : num;
} catch (e) { }
/**
* 精确乘法
*/
function times (num1, num2) {
var others = [];
for (var _i = 2; _i < arguments.length; _i++) {
others[_i - 2] = arguments[_i];
}
if (others.length > 0) {
return times.apply(void 0, [times(num1, num2), others[0]].concat(others.slice(1)));
}
var num1Changed = float2Fixed(num1);
var num2Changed = float2Fixed(num2);
var baseNum = digitLength(num1) + digitLength(num2);
var leftValue = num1Changed * num2Changed;
return leftValue / Math.pow(10, baseNum);
}
/**
* 精确除法
*/
function accDivide (num1, num2) {
var others = [];
for (var _i = 2; _i < arguments.length; _i++) {
others[_i - 2] = arguments[_i];
} }
with (Math) { if (others.length > 0) {
r1 = Number(arg1.toString().replace(".", "")); return accDivide.apply(void 0, [accDivide(num1, num2), others[0]].concat(others.slice(1)));
r2 = Number(arg2.toString().replace(".", ""));
return (t2 > t1) ? (r1 / r2) * pow(10, t2 - t1) : (r1 / r2) / pow(10, t1 - t2);
} }
var num1Changed = float2Fixed(num1);
var num2Changed = float2Fixed(num2);
return times((num1Changed / num2Changed), Math.pow(10, digitLength(num2) - digitLength(num1)));
} }
// 给Number类型增加一个div方法,调用起来更加方便。 // 给Number类型增加一个div方法,调用起来更加方便。
Number.prototype.div = function (arg) { Number.prototype.div = function (arg) {
return accDiv(this, arg); return accDivide(this, arg);
};/** };/**
* 对字符串对象的扩展 * 对字符串对象的扩展
* @class String * @class String

70
src/core/proto/number.js

@ -232,30 +232,64 @@ function accMul (arg1, arg2) {
Number.prototype.mul = function (arg) { Number.prototype.mul = function (arg) {
return accMul(arg, this); return accMul(arg, this);
}; };
/** /**
** 除法函数用来得到精确的除法结果 * Return digits length of a number
** 说明javascript的除法结果会有误差在两个浮点数相除的时候会比较明显这个函数返回较为精确的除法结果 * @param {*number} num Input number
** 调用accDiv(arg1,arg2) */
** 返回值arg1除以arg2的精确结果 function digitLength (num) {
**/ // Get digit length of e
function accDiv (arg1, arg2) { var eSplit = num.toString().split(/[eE]/);
var t1 = 0, t2 = 0, r1, r2; var len = (eSplit[0].split(".")[1] || "").length - (+(eSplit[1] || 0));
try { return len > 0 ? len : 0;
t1 = arg1.toString().split(".")[1].length; }
} catch (e) { /**
* 把小数转成整数支持科学计数法如果是小数则放大成整数
* @param {*number} num 输入数
*/
function float2Fixed (num) {
if (num.toString().indexOf("e") === -1) {
return Number(num.toString().replace(".", ""));
} }
try { var dLen = digitLength(num);
t2 = arg2.toString().split(".")[1].length; return dLen > 0 ? num * Math.pow(10, dLen) : num;
} catch (e) { }
/**
* 精确乘法
*/
function times (num1, num2) {
var others = [];
for (var _i = 2; _i < arguments.length; _i++) {
others[_i - 2] = arguments[_i];
}
if (others.length > 0) {
return times.apply(void 0, [times(num1, num2), others[0]].concat(others.slice(1)));
}
var num1Changed = float2Fixed(num1);
var num2Changed = float2Fixed(num2);
var baseNum = digitLength(num1) + digitLength(num2);
var leftValue = num1Changed * num2Changed;
return leftValue / Math.pow(10, baseNum);
}
/**
* 精确除法
*/
function accDivide (num1, num2) {
var others = [];
for (var _i = 2; _i < arguments.length; _i++) {
others[_i - 2] = arguments[_i];
} }
with (Math) { if (others.length > 0) {
r1 = Number(arg1.toString().replace(".", "")); return accDivide.apply(void 0, [accDivide(num1, num2), others[0]].concat(others.slice(1)));
r2 = Number(arg2.toString().replace(".", ""));
return (t2 > t1) ? (r1 / r2) * pow(10, t2 - t1) : (r1 / r2) / pow(10, t1 - t2);
} }
var num1Changed = float2Fixed(num1);
var num2Changed = float2Fixed(num2);
return times((num1Changed / num2Changed), Math.pow(10, digitLength(num2) - digitLength(num1)));
} }
// 给Number类型增加一个div方法,调用起来更加方便。 // 给Number类型增加一个div方法,调用起来更加方便。
Number.prototype.div = function (arg) { Number.prototype.div = function (arg) {
return accDiv(this, arg); return accDivide(this, arg);
}; };

70
utils/utils.js

@ -10416,32 +10416,66 @@ function accMul (arg1, arg2) {
Number.prototype.mul = function (arg) { Number.prototype.mul = function (arg) {
return accMul(arg, this); return accMul(arg, this);
}; };
/** /**
** 除法函数用来得到精确的除法结果 * Return digits length of a number
** 说明javascript的除法结果会有误差在两个浮点数相除的时候会比较明显这个函数返回较为精确的除法结果 * @param {*number} num Input number
** 调用accDiv(arg1,arg2) */
** 返回值arg1除以arg2的精确结果 function digitLength (num) {
**/ // Get digit length of e
function accDiv (arg1, arg2) { var eSplit = num.toString().split(/[eE]/);
var t1 = 0, t2 = 0, r1, r2; var len = (eSplit[0].split(".")[1] || "").length - (+(eSplit[1] || 0));
try { return len > 0 ? len : 0;
t1 = arg1.toString().split(".")[1].length; }
} catch (e) { /**
* 把小数转成整数支持科学计数法如果是小数则放大成整数
* @param {*number} num 输入数
*/
function float2Fixed (num) {
if (num.toString().indexOf("e") === -1) {
return Number(num.toString().replace(".", ""));
} }
try { var dLen = digitLength(num);
t2 = arg2.toString().split(".")[1].length; return dLen > 0 ? num * Math.pow(10, dLen) : num;
} catch (e) { }
/**
* 精确乘法
*/
function times (num1, num2) {
var others = [];
for (var _i = 2; _i < arguments.length; _i++) {
others[_i - 2] = arguments[_i];
}
if (others.length > 0) {
return times.apply(void 0, [times(num1, num2), others[0]].concat(others.slice(1)));
}
var num1Changed = float2Fixed(num1);
var num2Changed = float2Fixed(num2);
var baseNum = digitLength(num1) + digitLength(num2);
var leftValue = num1Changed * num2Changed;
return leftValue / Math.pow(10, baseNum);
}
/**
* 精确除法
*/
function accDivide (num1, num2) {
var others = [];
for (var _i = 2; _i < arguments.length; _i++) {
others[_i - 2] = arguments[_i];
} }
with (Math) { if (others.length > 0) {
r1 = Number(arg1.toString().replace(".", "")); return accDivide.apply(void 0, [accDivide(num1, num2), others[0]].concat(others.slice(1)));
r2 = Number(arg2.toString().replace(".", ""));
return (t2 > t1) ? (r1 / r2) * pow(10, t2 - t1) : (r1 / r2) / pow(10, t1 - t2);
} }
var num1Changed = float2Fixed(num1);
var num2Changed = float2Fixed(num2);
return times((num1Changed / num2Changed), Math.pow(10, digitLength(num2) - digitLength(num1)));
} }
// 给Number类型增加一个div方法,调用起来更加方便。 // 给Number类型增加一个div方法,调用起来更加方便。
Number.prototype.div = function (arg) { Number.prototype.div = function (arg) {
return accDiv(this, arg); return accDivide(this, arg);
};/** };/**
* 对字符串对象的扩展 * 对字符串对象的扩展
* @class String * @class String

Loading…
Cancel
Save