Search code examples
phpnumber-formatting

Convert Thousands in K format to regular Thousands format


I have numbers that are given to me in the following format:

12.2K

I would instead like to convert this number to display:

12200

The examples ive seen convert to K format, but I would like to convert from K format.

Is there an easy way to do this?

Thanks!


Solution

  • You mean, something like this? This will be able to convert thousands and millions, etc.

    <?php
      $s = "12.2K";
      if (strpos(strtoupper($s), "K") != false) {
        $s = rtrim($s, "kK");
        echo floatval($s) * 1000;
      } else if (strpos(strtoupper($s), "M") != false) {
        $s = rtrim($s, "mM");
        echo floatval($s) * 1000000;
      } else {
        echo floatval($s);
      }
    ?>