GeneralUtils/Profile: Shorten numbers to K M B format

- Implemented for currencies on the profile
This commit is contained in:
2023-07-31 15:52:42 +02:00
parent 724621f8da
commit e2dab416f2
2 changed files with 28 additions and 10 deletions

View File

@@ -17,5 +17,23 @@ module.exports = {
generateLogID: async function() {
return crypto.randomBytes(4).toString("hex");
},
formatNumber: function(num, precision = 1) {
const map = [
{ suffix: 'T', threshold: 1e12 },
{ suffix: 'B', threshold: 1e9 },
{ suffix: 'M', threshold: 1e6 },
{ suffix: 'K', threshold: 1e3 },
{ suffix: '', threshold: 1 },
];
const found = map.find((x) => Math.abs(num) >= x.threshold);
if (found) {
const formatted = (Math.floor((num / found.threshold)*10) / 10) + found.suffix;
return formatted;
}
return num;
}
}