Skip to content

Commit

Permalink
bump to 1.3.1
Browse files Browse the repository at this point in the history
  • Loading branch information
camsong committed Feb 14, 2019
1 parent 2df77f4 commit 58f5f04
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 15 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,10 @@ NP.divide(1.21, 1.1); // = 1.1, not 1.0999999999999999
NP.round(0.105, 2); // = 0.11, not 0.1
```

PS: if you want to get rid of `XXX is beyond boundary when transfer to integer, the results may not be accurate`, use this at the begining of your app to turn off boundary checking.
```js
NP.enableBoundaryChecking(false);
```

### License
MIT
20 changes: 16 additions & 4 deletions build/index.iife.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ var NP = (function (exports) {
'use strict';

/**
* @file 解决浮动运算问题,避免小数点后产生多位数和计算精度损失。
* @desc 解决浮动运算问题,避免小数点后产生多位数和计算精度损失。
* 问题示例:2.3 + 2.4 = 4.699999999999999,1.0 - 0.9 = 0.09999999999999998
*/
/**
Expand Down Expand Up @@ -39,8 +39,10 @@ function float2Fixed(num) {
* @param {*number} num 输入数
*/
function checkBoundary(num) {
if (num > Number.MAX_SAFE_INTEGER || num < Number.MIN_SAFE_INTEGER) {
console.warn(num + " is beyond boundary when transfer to integer, the results may not be accurate");
if (_boundaryCheckingState) {
if (num > Number.MAX_SAFE_INTEGER || num < Number.MIN_SAFE_INTEGER) {
console.warn(num + " is beyond boundary when transfer to integer, the results may not be accurate");
}
}
}
/**
Expand Down Expand Up @@ -113,7 +115,16 @@ function round(num, ratio) {
var base = Math.pow(10, ratio);
return divide(Math.round(times(num, base)), base);
}
var index = { strip: strip, plus: plus, minus: minus, times: times, divide: divide, round: round, digitLength: digitLength, float2Fixed: float2Fixed };
var _boundaryCheckingState = true;
/**
* 是否进行边界检查,默认开启
* @param flag 标记开关,true 为开启,false 为关闭,默认为 true
*/
function enableBoundaryChecking(flag) {
if (flag === void 0) { flag = true; }
_boundaryCheckingState = flag;
}
var index = { strip: strip, plus: plus, minus: minus, times: times, divide: divide, round: round, digitLength: digitLength, float2Fixed: float2Fixed, enableBoundaryChecking: enableBoundaryChecking };

exports.strip = strip;
exports.plus = plus;
Expand All @@ -123,6 +134,7 @@ exports.divide = divide;
exports.round = round;
exports.digitLength = digitLength;
exports.float2Fixed = float2Fixed;
exports.enableBoundaryChecking = enableBoundaryChecking;
exports['default'] = index;

return exports;
Expand Down
20 changes: 16 additions & 4 deletions build/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
Object.defineProperty(exports, '__esModule', { value: true });

/**
* @file 解决浮动运算问题,避免小数点后产生多位数和计算精度损失。
* @desc 解决浮动运算问题,避免小数点后产生多位数和计算精度损失。
* 问题示例:2.3 + 2.4 = 4.699999999999999,1.0 - 0.9 = 0.09999999999999998
*/
/**
Expand Down Expand Up @@ -40,8 +40,10 @@ function float2Fixed(num) {
* @param {*number} num 输入数
*/
function checkBoundary(num) {
if (num > Number.MAX_SAFE_INTEGER || num < Number.MIN_SAFE_INTEGER) {
console.warn(num + " is beyond boundary when transfer to integer, the results may not be accurate");
if (_boundaryCheckingState) {
if (num > Number.MAX_SAFE_INTEGER || num < Number.MIN_SAFE_INTEGER) {
console.warn(num + " is beyond boundary when transfer to integer, the results may not be accurate");
}
}
}
/**
Expand Down Expand Up @@ -114,7 +116,16 @@ function round(num, ratio) {
var base = Math.pow(10, ratio);
return divide(Math.round(times(num, base)), base);
}
var index = { strip: strip, plus: plus, minus: minus, times: times, divide: divide, round: round, digitLength: digitLength, float2Fixed: float2Fixed };
var _boundaryCheckingState = true;
/**
* 是否进行边界检查,默认开启
* @param flag 标记开关,true 为开启,false 为关闭,默认为 true
*/
function enableBoundaryChecking(flag) {
if (flag === void 0) { flag = true; }
_boundaryCheckingState = flag;
}
var index = { strip: strip, plus: plus, minus: minus, times: times, divide: divide, round: round, digitLength: digitLength, float2Fixed: float2Fixed, enableBoundaryChecking: enableBoundaryChecking };

exports.strip = strip;
exports.plus = plus;
Expand All @@ -124,4 +135,5 @@ exports.divide = divide;
exports.round = round;
exports.digitLength = digitLength;
exports.float2Fixed = float2Fixed;
exports.enableBoundaryChecking = enableBoundaryChecking;
exports['default'] = index;
2 changes: 1 addition & 1 deletion build/index.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 16 additions & 4 deletions build/index.umd.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
}(this, (function (exports) { 'use strict';

/**
* @file 解决浮动运算问题,避免小数点后产生多位数和计算精度损失。
* @desc 解决浮动运算问题,避免小数点后产生多位数和计算精度损失。
* 问题示例:2.3 + 2.4 = 4.699999999999999,1.0 - 0.9 = 0.09999999999999998
*/
/**
Expand Down Expand Up @@ -42,8 +42,10 @@ function float2Fixed(num) {
* @param {*number} num 输入数
*/
function checkBoundary(num) {
if (num > Number.MAX_SAFE_INTEGER || num < Number.MIN_SAFE_INTEGER) {
console.warn(num + " is beyond boundary when transfer to integer, the results may not be accurate");
if (_boundaryCheckingState) {
if (num > Number.MAX_SAFE_INTEGER || num < Number.MIN_SAFE_INTEGER) {
console.warn(num + " is beyond boundary when transfer to integer, the results may not be accurate");
}
}
}
/**
Expand Down Expand Up @@ -116,7 +118,16 @@ function round(num, ratio) {
var base = Math.pow(10, ratio);
return divide(Math.round(times(num, base)), base);
}
var index = { strip: strip, plus: plus, minus: minus, times: times, divide: divide, round: round, digitLength: digitLength, float2Fixed: float2Fixed };
var _boundaryCheckingState = true;
/**
* 是否进行边界检查,默认开启
* @param flag 标记开关,true 为开启,false 为关闭,默认为 true
*/
function enableBoundaryChecking(flag) {
if (flag === void 0) { flag = true; }
_boundaryCheckingState = flag;
}
var index = { strip: strip, plus: plus, minus: minus, times: times, divide: divide, round: round, digitLength: digitLength, float2Fixed: float2Fixed, enableBoundaryChecking: enableBoundaryChecking };

exports.strip = strip;
exports.plus = plus;
Expand All @@ -126,6 +137,7 @@ exports.divide = divide;
exports.round = round;
exports.digitLength = digitLength;
exports.float2Fixed = float2Fixed;
exports.enableBoundaryChecking = enableBoundaryChecking;
exports['default'] = index;

Object.defineProperty(exports, '__esModule', { value: true });
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "number-precision",
"version": "1.2.1",
"version": "1.3.1",
"description": "Perform addition, subtraction, multiplication and division operations precisely using javascript",
"main": "build/index.js",
"types": "src/index.ts",
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ function round(num: number, ratio: number): number {
return divide(Math.round(times(num, base)), base);
}

let _boundaryCheckingState = false;
let _boundaryCheckingState = true;
/**
* 是否进行边界检查,默认开启
* @param flag 标记开关,true 为开启,false 为关闭,默认为 true
Expand Down

0 comments on commit 58f5f04

Please sign in to comment.