Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
908 B
1
Indexable
Never
function numberToK(num) {
    if (num >= 1000) {
        num = num / 1000;
        if (num >= 1 && num < 10) {
            return num.toFixed(1) + "k";
        } else if (num >= 10 && num < 1000) {
            return Math.floor(num) + "k+";
        } else if (num >= 1000 && num < 1000000) {
            return Math.floor(num / 1000) + "M+";
        } else if (num >= 1000000 && num < 1000000000) {
            return Math.floor(num / 1000000) + "B+";
        } else if (num >= 1000000000 && num < 1000000000000) {
            return Math.floor(num / 1000000000) + "T+";
        }
    } else {
        return num.toString();
    }
}

console.log(numberToK(30000)); // Output: 30k
console.log(numberToK(200000)); // Output: 200k+
console.log(numberToK(30000000)); // Output: 30M+
console.log(numberToK(400000000)); // Output: 400B+
console.log(numberToK(500000000000)); // Output: 500T+