createRound.js 620 B

1234567891011121314151617181920212223
  1. /** Native method references. */
  2. var pow = Math.pow;
  3. /**
  4. * Creates a `_.ceil`, `_.floor`, or `_.round` function.
  5. *
  6. * @private
  7. * @param {string} methodName The name of the `Math` method to use when rounding.
  8. * @returns {Function} Returns the new round function.
  9. */
  10. function createRound(methodName) {
  11. var func = Math[methodName];
  12. return function(number, precision) {
  13. precision = precision === undefined ? 0 : (+precision || 0);
  14. if (precision) {
  15. precision = pow(10, precision);
  16. return func(number * precision) / precision;
  17. }
  18. return func(number);
  19. };
  20. }
  21. module.exports = createRound;