Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PHP

PHP Number Shortener

<?php 
function number_shorten($number, $precision = 3, $divisors = null) {
    if (!isset($divisors)) {
        $divisors = array(
            pow(1000, 0) => '', // 1000^0 == 1
            pow(1000, 1) => 'K', // Thousand
            pow(1000, 2) => 'M', // Million
            pow(1000, 3) => 'B', // Billion
            pow(1000, 4) => 'T', // Trillion
            pow(1000, 5) => 'QA', // Quadrillion
            pow(1000, 6) => 'QI', // Quintillion
        );
    }
    foreach ($divisors as $divisor => $shorthand) {
        if (abs($number) < ($divisor * 1000)) {
            // We found a match!
            break;
        }
    }
    return number_format($number / $divisor, $precision) . $shorthand;
}

echo number_shorten(12345678901234567890);
?>
 
PREVIOUS NEXT
Tagged: #PHP #Number #Shortener
ADD COMMENT
Topic
Name
2+6 =