[Solved] Number formatting


Just change your if condition and remove 1x zero, so from this:

if ($number < 10000) {
    return pm_number_format($number);
}

to this:

if ($number < 1000) {
    return pm_number_format($number);
}

Input:

1
12
123
1234
12345
123456
1234567
12345678
123456789

Output:

1
12
123
1.2K  //<--See output as you wanted
12.3K
123.5K
1.2M
12.3M
123.5M

EDIT:

Here is my code i modify(like i described up above) and used to produce the output:

<?php

    function pm_number_format($number, $decimals = 0, $dec_point=".", $thousands_sep = ',') {
        return number_format($number, $decimals, $dec_point, $thousands_sep);
    }

    function pm_compact_number_format($number) {
        if ($number < 1000)
            return pm_number_format($number);

        $d = $number < 1000000 ? 1000 : 1000000;
        $f = round($number / $d, 1);

        return pm_number_format($f, $f - intval($f) ? 1 : 0) . ($d == 1000 ? 'k' : 'M');
    }


    $number = "";

    foreach(range(1,10) as $value) {
        $number .= $value;
        echo pm_compact_number_format($number) . "<br />";
    }

?>

solved Number formatting <-