Sha256: 1c9276772edaad68cbc34e71cb3ebcbe92a77ae1a990dcee77cbc117831e2efe

Contents?: true

Size: 1.07 KB

Versions: 100

Compression:

Stored size: 1.07 KB

Contents

var toInteger = require('./toInteger'),
    toNumber = require('./toNumber'),
    toString = require('./toString');

/* Built-in method references for those with the same name as other `lodash` methods. */
var nativeMin = Math.min;

/**
 * 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 = precision == null ? 0 : nativeMin(toInteger(precision), 292);
    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);
  };
}

module.exports = createRound;

Version data entries

100 entries across 100 versions & 12 rubygems

Version Path
sumomo-0.10.8 data/sumomo/api_modules/node_modules/lodash/_createRound.js
sumomo-0.10.4 data/sumomo/api_modules/node_modules/lodash/_createRound.js
sumomo-0.10.3 data/sumomo/api_modules/node_modules/lodash/_createRound.js
sumomo-0.10.2 data/sumomo/api_modules/node_modules/lodash/_createRound.js
sumomo-0.10.1 data/sumomo/api_modules/node_modules/lodash/_createRound.js
sumomo-0.10.0 data/sumomo/api_modules/node_modules/lodash/_createRound.js
sumomo-0.9.0 data/sumomo/api_modules/node_modules/lodash/_createRound.js
sumomo-0.8.22 data/sumomo/api_modules/node_modules/lodash/_createRound.js
sumomo-0.8.21 data/sumomo/api_modules/node_modules/lodash/_createRound.js
sumomo-0.8.20 data/sumomo/api_modules/node_modules/lodash/_createRound.js
sumomo-0.8.17 data/sumomo/api_modules/node_modules/lodash/_createRound.js
sumomo-0.8.16 data/sumomo/api_modules/node_modules/lodash/_createRound.js
sumomo-0.8.15 data/sumomo/api_modules/node_modules/lodash/_createRound.js
sumomo-0.8.14 data/sumomo/api_modules/node_modules/lodash/_createRound.js
sumomo-0.8.13 data/sumomo/api_modules/node_modules/lodash/_createRound.js
sumomo-0.8.12 data/sumomo/api_modules/node_modules/lodash/_createRound.js
sumomo-0.8.11 data/sumomo/api_modules/node_modules/lodash/_createRound.js
sumomo-0.8.10 data/sumomo/api_modules/node_modules/lodash/_createRound.js
sumomo-0.8.9 data/sumomo/api_modules/node_modules/lodash/_createRound.js
sumomo-0.8.8 data/sumomo/api_modules/node_modules/lodash/_createRound.js