mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-08 10:17:48 +00:00
Bump to v4.1.0.
This commit is contained in:
29
_createRound.js
Normal file
29
_createRound.js
Normal file
@@ -0,0 +1,29 @@
|
||||
define(['./toInteger', './toNumber', './toString'], function(toInteger, toNumber, toString) {
|
||||
|
||||
/**
|
||||
* Creates a function like `_.round`.
|
||||
*
|
||||
* @private
|
||||
* @param {string} methodName The name of the `Math` method to use when rounding.
|
||||
* @returns {Function} Returns the new round function.
|
||||
*/
|
||||
function createRound(methodName) {
|
||||
var func = Math[methodName];
|
||||
return function(number, precision) {
|
||||
number = toNumber(number);
|
||||
precision = toInteger(precision);
|
||||
if (precision) {
|
||||
// Shift with exponential notation to avoid floating-point issues.
|
||||
// See [MDN](https://mdn.io/round#Examples) for more details.
|
||||
var pair = (toString(number) + 'e').split('e'),
|
||||
value = func(pair[0] + 'e' + (+pair[1] + precision));
|
||||
|
||||
pair = (toString(value) + 'e').split('e');
|
||||
return +(pair[0] + 'e' + (+pair[1] - precision));
|
||||
}
|
||||
return func(number);
|
||||
};
|
||||
}
|
||||
|
||||
return createRound;
|
||||
});
|
||||
Reference in New Issue
Block a user