Last active
July 2, 2021 02:17
-
-
Save A-limon/f16b18c464bdb78603e5 to your computer and use it in GitHub Desktop.
解决JavaScript浮点数计算的问题
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;(function (root, factory) { | |
if (typeof define === 'function' && define.amd) { | |
define(factory); | |
} else if (typeof exports === 'object') { | |
module.exports = factory(); | |
} else { | |
root._Math = factory(); | |
} | |
}(this, function() { | |
var _Math = { | |
_getDecimalLength : function(value) { | |
var list = (value + '').split('.'), | |
result = 0; | |
if (list[1] !== undefined && list[1].length > 0) { | |
result = list[1].length; | |
} | |
return result; | |
}, | |
mul: function (num1,num2) { | |
var intValue1 = Number(num1.toString().replace('.', '')), | |
intValue2 = Number(num2.toString().replace('.', '')), | |
decimalLength = _Math._getDecimalLength(num1) + _Math._getDecimalLength(num2); | |
return intValue1 * intValue2 / Math.pow(10, decimalLength); | |
}, | |
add: function (num1,num2) { | |
var baseNum = Math.pow(10, Math.max(_Math._getDecimalLength(num1), _Math._getDecimalLength(num2))); | |
return (_Math.mul(num1,baseNum) + _Math.mul(num2,baseNum)) / baseNum; | |
}, | |
sub: function (num1,num2) { | |
var baseNum = Math.pow(10, Math.max(_Math._getDecimalLength(num1), _Math._getDecimalLength(num2))); | |
return ((_Math.mul(num1,baseNum) - _Math.mul(num2,baseNum)) / baseNum); | |
}, | |
div: function (num1,num2) { | |
var intValue1 = Number(num1.toString().replace('.', '')), | |
intValue2 = Number(num2.toString().replace('.', '')); | |
return (intValue1 / intValue2) * Math.pow(10, _Math._getDecimalLength(num2) - _Math._getDecimalLength(num1)); | |
} | |
} | |
return _Math; | |
})); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment