-1

I have beginners skills with PHP and hence need your help guys.

What i am trying to achieve is to round up a number for total shares count for a plugin.

my variable is $totalshare
i am using return $totalshare to dispaly the results

i would like to achieve results as following example:

1000 = 1k
1.230 = 1.2k
1489 = 1.5k
1.660 = 1.6k
.
.
.
15576 = 15.6k
.
.
1201200 = 12.02mil
1211200 = 12.12mil

Any help would be much appreciated. Thanks in advance.

Manu
  • 806
  • 1
  • 9
  • 25
  • 1
    possible duplicate of [PHP Count round thousand to a K style count like facebook Share . . . Twitter Button ect](http://stackoverflow.com/questions/4116499/php-count-round-thousand-to-a-k-style-count-like-facebook-share-twitter-bu) – Black Sheep Sep 06 '14 at 14:13
  • or http://stackoverflow.com/questions/16292304/php-count-round-thousand-to-a-k-style-count-facebook-share – Black Sheep Sep 06 '14 at 14:14
  • those answers does not round up to two decimal places, and iv tried them before posting this question, did not work. – Manu Sep 06 '14 at 14:18
  • or http://stackoverflow.com/a/2704122/2097224 – Black Sheep Sep 06 '14 at 14:37
  • The principal question is here: What have you tried? SO is not a platform for getting things done your homework. – Black Sheep Sep 06 '14 at 14:44

2 Answers2

0

Simplest way:

if($n>=1000) $n = ($n/1000)."k";

if you want something more i can edit

Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
0

You can write like this,

function convertShares($shareValue){
    if($shareValue < 1000){
    return shareValue;
    }elseif($shareValue > 100000 ){
          return number_format($shareValue/100000,2) . "mil";
    }else{
        return number_format($shareValue/1000,2) . "k";
    }
  } 
Pundit
  • 229
  • 2
  • 7