-1

I'd like to convert currency values like this:

4 000 000 000 => 4b
2 000 000 => 2m
2 250 000 => 2.25m
195 000 => 195k
10 000 => 10k

Anyone know of an existing library/function that does this, or do I need to write this myself by means of conditionals that cater for 0-9999, 10000-999999, and so on..?

Mark
  • 407
  • 7
  • 17
  • You can probably just count the zeroes, and divide by three to get a value to map to K, M, etc. Watch out for billion though - I think the number of zeroes on that is dependent on territory. Doesn't the US regard that as million million? (Edit: [it's changed](https://en.wikipedia.org/wiki/1,000,000,000)). – halfer Jun 30 '15 at 11:21
  • 1
    Not the US, no: they use the "short scale". You need distinguish between "short scale" and "long scale": not just billion is affected. I recall that currency *always* uses the short scale but can't find a citation: perhaps I've imagined that. – Bathsheba Jun 30 '15 at 11:23
  • (Explaining the likely reason for the downvotes: we have a longstanding policy of showing what you have tried). – halfer Jun 30 '15 at 12:03

2 Answers2

2

You can create a function like this to display shorten the values but it would miss the exact value

function shortifyCurrency($var){
  $var=doubleval($var);
  if($var>=1000000000){
    $var=($var/1000000000)."b";
  }else if($var>=1000000){
    $var=($var/1000000)."m";
  }else if($var>=1000){
    $var=($var/1000)."k";
  }
  return $var;
}
//Output 4b
echo shortifyCurrency(4000000000);

Or you could use the following function to display more detailed information about the value

function shortifyCurrencyXtream($var){
  $var=doubleval($var);
  $print_str="";

  if($var>=1000000000){
    $print_str.=round($var/1000000000)."b";
    $var=$var%1000000000;
  }
  if($var>=1000000){
    if($print_str!="") $print_str.=" ";
    $print_str.=round($var/1000000)."m";
    $var=$var%1000000;
  }
  if($var>=1000){
    if($print_str!="") $print_str.=" ";
    $print_str.=($var/1000)."k";
  }
  return $print_str;
}
//Output 4b 101m 500.3k
echo shortifyCurrencyXtream(4100500300);
knetsi
  • 1,601
  • 1
  • 16
  • 18
2

You can create a custom function for this. The below one, firstly removes everything but numbers from string. Then it formats the number according how big is it and to the digit provided.

function shorten($num, $digits = 1) {
    $num = preg_replace('/[^0-9]/','',$num);
    if ($num >= 1000000000) {
        $num = number_format(($num / 1000000000), $digits, '.', '') + 0;
        $num = $num . "b";
    }
    if ($num >= 1000000) {
        $num = number_format(($num / 1000000), $digits, '.', '') + 0;
        $num = $num . 'm';
    }
    if ($num >= 1000) {
        $num = number_format(($num / 1000), $digits, '.', '') + 0;
        $num = $num . 'k';
    }
    return $num;
}

echo shorten("4 000 000 000");
echo shorten("3 200 000 000");
echo shorten("195 000");

Demo: http://codepad.org/K971MzVx

Burak
  • 5,252
  • 3
  • 22
  • 30
  • Thanks @Burak. This is so close... One small thing is when I shorten 17000, I get 17.0k. This is right when the value is 17500 for example, but if its a zero, it should just be 17k – Mark Jun 30 '15 at 11:48
  • Likewise if I do 70000, it returns 70.0k – Mark Jun 30 '15 at 11:50
  • edited the answer according to that, just add zero to the number, then you are fine. – Burak Jun 30 '15 at 11:55
  • if I use 3750000 it becomes 3.8m. Is there a way to have it *not* round up? – Mark Jun 30 '15 at 12:12
  • see this: http://stackoverflow.com/questions/3833137/how-to-make-number-format-not-to-round-numbers-up – Burak Jun 30 '15 at 12:15