10

Edit: Function below now does the abbreviation correctly, implemented @Asad 's solution

Hi I am currently working on a like button, I've got all the base functionality working nicely however I have started the number abbreviation code and hit a wall as I can't figure out how to make the abbreviation more precise.

I have a number, e.g. 1000, 1230, 1500, 154000, 1500000, 1000000

And I want to format them with an abbreviation. i.e.

if it's a thousand, then 1k, 1.1k, 2k, 10k, 10.5k etc...

and so on for the tens, hundreds thousands and millions, etc...

At the moment I have the following function but it's not specific enough:

function abreviateTotalCount($value) 
{

    $abbreviations = array(12 => 'T', 9 => 'B', 6 => 'M', 3 => 'K', 0 => '');

    foreach($abbreviations as $exponent => $abbreviation) 
    {

        if($value >= pow(10, $exponent)) 
        {

            return round(floatval($value / pow(10, $exponent))).$abbreviation;

        }

    }

}

Thanks in advance!

dnagirl
  • 20,196
  • 13
  • 80
  • 123
André Figueira
  • 6,048
  • 14
  • 48
  • 62

6 Answers6

7

If you want to keep the decimal places, use floatval instead of intval:

return round(floatval($value / pow(10, $exponent)),1).$abbreviation;

gets the float representation and rounds to 1 decimal place.

Asad Saeeduddin
  • 46,193
  • 6
  • 90
  • 139
2

check that project https://github.com/gburtini/Humanize-PHP/blob/master/Humanize.php

php > echo HumanizePHP::intword(256,0,0);
3 hundred
php > echo HumanizePHP::intword(256,0,1);
2.6 hundred
php > echo HumanizePHP::intword(256,0,2);
2.56 hundred

At least you can take idea from this implementation

Fivell
  • 11,829
  • 3
  • 61
  • 99
1

Instead of intval(), use number_format() to give you a number with the desired amount of decimal places.

bobwienholt
  • 17,420
  • 3
  • 40
  • 48
0

You can Rather Use these Solution from GITHUB https://github.com/prezine/numb

$numb = new numbConverter() echo $numb->__main('1400000'); //output => 1.4M

A Very easy to Use Solution

Precious Tom
  • 486
  • 3
  • 18
  • 7 year old question bro, also, the accepted answer is simpler than including an entire package for something so trivial, in addition, your package includes the code for several languages, which is bad practice. – André Figueira Jul 12 '18 at 13:14
  • Uhmm, newcomers like me could stumble on this question and want an answer, plus it contains several languages, just if you wish to use either of the available... – Precious Tom Jul 12 '18 at 13:25
  • The point is we would want you to use the accepted answer instead, this is a little bit more of you plugging your library no? :) don't get me wrong, get involved, etc... but try to answer the question here, rather than plugging a library you've built. Such as by maybe showing the code in the library you used to acheive it, as that will form a better answer, which more people are likely to upvote ;) – André Figueira Jul 12 '18 at 15:33
  • Lol', Okay... Noted. Before the More people, be the one person to Upvote... while I edit the answer to be formative rather than advertising – Precious Tom Jul 12 '18 at 15:56
0

If you ever wish to support 0, negative numbers and bigger numbers like Decillions you can try this one :)

function numberAbbreviation($number, $floating_points = 1) {

    $a = ['', 'K', 'M', 'B', 't', 'q', 'Q', 's', 'S', 'o', 'n', 'd'];
    $n = intval($number);
    $i = intdiv(strlen((string) $n) - 1, 3);
    $m = isset($a[$i]) ? $a[$i] : '';
    $e = $floating_points;

    return number_format($n / (1000 ** $i), $m ? $e : 0) . $m;
}
sulest
  • 1,006
  • 1
  • 10
  • 17
0

I made a function than is simple to understand and is easily extended to more suffixes:

0: 0
1,000: 1 000
10,000: 10 K
10,100: 10.1 K
100,000: 100 K
101,500: 101 K

function abbreviate_numbers(int $num){
        $num_len = strlen($num);
        switch($num_len){
            case 1: return $num;
            case 2: return $num;
            case 3: return $num;
            case 4: return number_format($num, 0, ".", " ");
            case 5: return intval(substr($num, 0, -2)) / 10 . " K";
            case 6: return intdiv($num, 1000) . " K";
            case 7: return intval(substr($num, 0, -5)) / 10 . " M";
            case 8: return intval(substr($num, 0, -5)) / 10 . " M";
            case 9: return intdiv($num, 1000000) . " M";
            case 10: return intval(substr($num, 0, -8)) / 10 . " B";
            case 11: return intval(substr($num, 0, -8)) / 10 . " B";
            case 12: return intdiv($num, 1000000000) . " B";
            case 13: return intval(substr($num, 0, -11)) / 10 . " T";
            case 14: return intval(substr($num, 0, -11)) / 10 . " T";
            case 15: return intdiv($num, 1000000000000) . " T";
            case 16: return intval(substr($num, 0, -14)) / 10 . " Q";
            case 17: return intval(substr($num, 0, -14)) / 10 . " Q";
            case 18: return intdiv($num, 1000000000000000) . " Q";
            default: return number_format($num, 0, ".", " ");
        }
    }
James Risner
  • 5,451
  • 11
  • 25
  • 47