_decimal-adjust.js 808 B

1234567891011121314151617181920212223242526272829
  1. // Credit:
  2. // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/round
  3. // #Decimal_rounding
  4. "use strict";
  5. var isValue = require("../object/is-value")
  6. , ensureInteger = require("../object/ensure-integer");
  7. var split = String.prototype.split;
  8. module.exports = function (type) {
  9. return function (value/*, exp*/) {
  10. value = Number(value);
  11. var exp = arguments[1];
  12. if (isValue(exp)) exp = ensureInteger(exp);
  13. if (!value) return value;
  14. if (!exp) return Math[type](value);
  15. if (!isFinite(value)) return value;
  16. // Shift
  17. var tokens = split.call(value, "e");
  18. value = Math[type](tokens[0] + "e" + ((tokens[1] || 0) - exp));
  19. // Shift back
  20. tokens = value.toString().split("e");
  21. return Number(tokens[0] + "e" + (Number(tokens[1] || 0) + exp));
  22. };
  23. };