main
Last change
on this file was d565449, checked in by stefan toskovski <stefantoska84@…>, 4 weeks ago |
Update repo after prototype presentation
|
-
Property mode
set to
100644
|
File size:
1.2 KB
|
Line | |
---|
1 | import root from './_root.js';
|
---|
2 | import toInteger from './toInteger.js';
|
---|
3 | import toNumber from './toNumber.js';
|
---|
4 | import toString from './toString.js';
|
---|
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 | export default createRound;
|
---|
Note:
See
TracBrowser
for help on using the repository browser.