number.js 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.mid = exports.multi = exports.ifPositive = exports.ifNegative = exports.toKNotation = exports.toScientificNotation = exports.toThousands = exports.toPrecision = void 0;
  4. /**
  5. * 保留x位小数
  6. */
  7. function toPrecision(num, precision) {
  8. var result = Math.pow(10, precision);
  9. // eslint-disable-next-line
  10. return ~~(num * result) / result;
  11. }
  12. exports.toPrecision = toPrecision;
  13. /**
  14. * 千分位
  15. * 100000 -> 10,000
  16. */
  17. function toThousands(num) {
  18. return num.toLocaleString();
  19. }
  20. exports.toThousands = toThousands;
  21. /**
  22. * 获得数字科学计数
  23. * 1000000 = 1e6
  24. */
  25. function toScientificNotation(num) {
  26. return num.toExponential();
  27. }
  28. exports.toScientificNotation = toScientificNotation;
  29. /**
  30. * 用k的方式表达
  31. * 1234 -> 1K
  32. * 12345 -> 12K
  33. */
  34. function toKNotation(num, precision) {
  35. if (precision === void 0) { precision = 0; }
  36. if (Math.abs(num) < 1000)
  37. return String(num);
  38. return "".concat(toPrecision(num / 1000, precision).toLocaleString(), "K");
  39. }
  40. exports.toKNotation = toKNotation;
  41. // Condition if x is smaller than zero.
  42. var ifNegative = function (x, a, b) { return (x < 0 && Number.isFinite(x) ? a : b); };
  43. exports.ifNegative = ifNegative;
  44. // Condition if x is greater than zero.
  45. var ifPositive = function (x, a, b) { return (x > 0 && Number.isFinite(x) ? a : b); };
  46. exports.ifPositive = ifPositive;
  47. // Calculate the result of a * b.
  48. var multi = function (a, b) { return a * b; };
  49. exports.multi = multi;
  50. // Calculate the result of (a + b) / 2.
  51. var mid = function (a, b) { return a / 2 + (b || 0) / 2; };
  52. exports.mid = mid;
  53. //# sourceMappingURL=number.js.map