| 1 |
var root = require('./_root'), |
| 2 |
toInteger = require('./toInteger'), |
| 3 |
toNumber = require('./toNumber'), |
| 4 |
toString = require('./toString'); |
| 5 |
|
| 6 |
/* Built-in method references for those with the same name as other `lodash` methods. */ |
| 7 |
var nativeIsFinite = root.isFinite, |
| 8 |
nativeMin = Math.min; |
| 9 |
|
| 10 |
/** |
| 11 |
* Creates a function like `_.round`. |
| 12 |
* |
| 13 |
* @private |
| 14 |
* @param {string} methodName The name of the `Math` method to use when rounding. |
| 15 |
* @returns {Function} Returns the new round function. |
| 16 |
*/ |
| 17 |
function createRound(methodName) { |
| 18 |
var func = Math[methodName]; |
| 19 |
return function(number, precision) { |
| 20 |
number = toNumber(number); |
| 21 |
precision = precision == null ? 0 : nativeMin(toInteger(precision), 292); |
| 22 |
if (precision && nativeIsFinite(number)) { |
| 23 |
// Shift with exponential notation to avoid floating-point issues. |
| 24 |
// See [MDN](https://mdn.io/round#Examples) for more details. |
| 25 |
var pair = (toString(number) + 'e').split('e'), |
| 26 |
value = func(pair[0] + 'e' + (+pair[1] + precision)); |
| 27 |
|
| 28 |
pair = (toString(value) + 'e').split('e'); |
| 29 |
return +(pair[0] + 'e' + (+pair[1] - precision)); |
| 30 |
} |
| 31 |
return func(number); |
| 32 |
}; |
| 33 |
} |
| 34 |
|
| 35 |
module.exports = createRound; |