makit
Version:
Make in JavaScript done right!
36 lines (35 loc) • 953 B
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.relation = exports.humanReadable = void 0;
/**
* 输出可读的数字,例如:
*
* 123456 -> 123,456
* 1234.5 -> 1,234.5
*/
function humanReadable(n) {
const [wholePart, decimalPart] = String(n).split('.');
let result = decimalPart ? '.' + decimalPart : '';
for (let i = wholePart.length - 1; i >= 0; i--) {
if (wholePart[i] >= '0' && wholePart[i] <= '9' &&
i < wholePart.length - 1 && (wholePart.length - 1 - i) % 3 === 0) {
result = ',' + result;
}
result = wholePart[i] + result;
}
return result;
}
exports.humanReadable = humanReadable;
/**
* 数字关系的字符表示,用于日志
*/
function relation(lhs, rhs) {
if (lhs > rhs)
return '>';
if (lhs < rhs)
return '<';
if (lhs === rhs)
return '=';
return '?';
}
exports.relation = relation;