| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253 |
- "use strict";
- Object.defineProperty(exports, "__esModule", { value: true });
- exports.mid = exports.multi = exports.ifPositive = exports.ifNegative = exports.toKNotation = exports.toScientificNotation = exports.toThousands = exports.toPrecision = void 0;
- /**
- * 保留x位小数
- */
- function toPrecision(num, precision) {
- var result = Math.pow(10, precision);
- // eslint-disable-next-line
- return ~~(num * result) / result;
- }
- exports.toPrecision = toPrecision;
- /**
- * 千分位
- * 100000 -> 10,000
- */
- function toThousands(num) {
- return num.toLocaleString();
- }
- exports.toThousands = toThousands;
- /**
- * 获得数字科学计数
- * 1000000 = 1e6
- */
- function toScientificNotation(num) {
- return num.toExponential();
- }
- exports.toScientificNotation = toScientificNotation;
- /**
- * 用k的方式表达
- * 1234 -> 1K
- * 12345 -> 12K
- */
- function toKNotation(num, precision) {
- if (precision === void 0) { precision = 0; }
- if (Math.abs(num) < 1000)
- return String(num);
- return "".concat(toPrecision(num / 1000, precision).toLocaleString(), "K");
- }
- exports.toKNotation = toKNotation;
- // Condition if x is smaller than zero.
- var ifNegative = function (x, a, b) { return (x < 0 && Number.isFinite(x) ? a : b); };
- exports.ifNegative = ifNegative;
- // Condition if x is greater than zero.
- var ifPositive = function (x, a, b) { return (x > 0 && Number.isFinite(x) ? a : b); };
- exports.ifPositive = ifPositive;
- // Calculate the result of a * b.
- var multi = function (a, b) { return a * b; };
- exports.multi = multi;
- // Calculate the result of (a + b) / 2.
- var mid = function (a, b) { return a / 2 + (b || 0) / 2; };
- exports.mid = mid;
- //# sourceMappingURL=number.js.map
|