How to format a number as 2.5K if a thousand in PHP? Like Facebook

XeanNeia

I need to show a page views value in the format of 1K of equal to one thousand, or 1.1K, 1.2K, 1.9K etc, if its not an even thousands, otherwise if under a thousand, display normal 500, 100, 250 etc, using PHP to format the number?

I'm using:--

function count_number($n) {
    // first strip any formatting;
    $n = (0+str_replace(",","",$n));

    // is this a number?
    if(!is_numeric($n)) return false;

    // now filter it;
    if($n>1000000000000) return round(($n/1000000000000),1).'T';
    else if($n>1000000000) return round(($n/1000000000),1).'G';
    else if($n>1000000) return round(($n/1000000),1).'M';
    else if($n>1000) return round(($n/1000),1).'K';

    return number_format($n);
}

BUT it does not work correctly...

If my page visted 2454 times, it shows 2.5k and if 2990, it shows 3k...

How o fix that problem??

I want to SHOW Like --> if page visited 2454 -> how to display 2.4k and if 2990 -> 2.9k, if 3000 -> 3k etc

Plz help me...


Thanks @ MonkeyZeus

Now itz DONE...

function kilo_mega_giga($n) {    

if($n >= 1000 && $n < 1000000)
{
    if($n%1000 === 0)
    {
        $formatted = ($n/1000);
    }
    else
    {
        $formatted = substr($n, 0, -3).'.'.substr($n, -3, 1);
         if(substr($formatted, -1, 1) === '0')
         {
           $formatted = substr($formatted, 0, -2);
         }
    }

    $formatted.= 'k';

} else 

if($n >= 1000000 && $n < 1000000000)
{
    if($n%1000000 === 0)
    {
        $formatted = ($n/1000000);
    }
    else
    {
        $formatted = substr($n, 0, -6).'.'.substr($n, -6, 1);
         if(substr($formatted, -1, 1) === '0')
         {
           $formatted = substr($formatted, 0, -2);
         }
    }

    $formatted.= 'M';
} else 

if($n >= 1000000000 && $n < 1000000000000)
{
    if($n%1000000000 === 0)
    {
        $formatted = ($n/1000000000);
    }
    else
    {
        $formatted = substr($n, 0, -9).'.'.substr($n, -9, 1);
         if(substr($formatted, -1, 1) === '0')
         {
           $formatted = substr($formatted, 0, -2);
         }
    }

    $formatted.= 'G';
} else

if($n >= 0 && $n < 1000)
{ 

    $formatted= $n;
} 

return $formatted;

}
MonkeyZeus

You can use this to calculate thousands. You can use this formula to figure out the formula for millions as well.

$n = 2000;
$formatted = '';

if($n >= 1000 && $n < 1000000)
{
    if($n%1000 === 0)
    {
        $formatted = ($n/1000);
    }
    else
    {
        $formatted = substr($n, 0, -3).'.'.substr($n, -3, -2);

        if(substr($formatted, -1, 1) === '0')
        {
            $formatted = substr($formatted, 0, -2);
        }
    }

    $formatted.= 'k';
}

echo $formatted;

Also, please use curly braces, ALWAYS. Future you will thank present you.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to format number with "." as thousand separator, and "," as decimal separator?

From Dev

How to format a number with spanish thousand grouping in python?

From Dev

How to format a number with spanish thousand grouping in python?

From Dev

Format number with space as thousand separator

From Dev

Format number with space as thousand separator

From Dev

String Number format with "/" for thousand separator

From Java

How to show thousand in k format for bar values in chart.js

From Dev

ionic +number format trouble based on the thousand separator

From Dev

Format integer number with . as thousand separator and no decimals

From Dev

Set standard number format with thousand separator, 2 decimals and minus sign for negative numbers using VBA?

From Dev

How format number value in php?

From Dev

How to remove thousand separaters in number using javascript?

From Dev

Format a number to display a comma when larger than a thousand

From Dev

How do I get the number of Facebook-like for each picture?

From Dev

How to format number 300 to 3.0 in php

From Dev

How to convert price format to number only with php?

From Dev

How to convert a number to a specific format in PHP?

From Dev

how to change long format number in php?

From Dev

PHP add thousand separators to number and keep leading zeros

From Dev

How to create static CSRF token in php like facebook and Google?

From Dev

Angular 2: pipes - How to format a phone number?

From Dev

How to format a decimal number to 2 or 3 digits?

From Dev

How to generate a file format like this (javascript array??) ?? in PHP

From Dev

PHP number format with integer format

From Dev

PHP number format with integer format

From Dev

asp.net mvc 5 how to set number into long value like 101-2esdf-1212

From Dev

How to use Facebook PHP SDK with Yii 2?

From Dev

Number format display in php

From Dev

PHP - change number format

Related Related

  1. 1

    How to format number with "." as thousand separator, and "," as decimal separator?

  2. 2

    How to format a number with spanish thousand grouping in python?

  3. 3

    How to format a number with spanish thousand grouping in python?

  4. 4

    Format number with space as thousand separator

  5. 5

    Format number with space as thousand separator

  6. 6

    String Number format with "/" for thousand separator

  7. 7

    How to show thousand in k format for bar values in chart.js

  8. 8

    ionic +number format trouble based on the thousand separator

  9. 9

    Format integer number with . as thousand separator and no decimals

  10. 10

    Set standard number format with thousand separator, 2 decimals and minus sign for negative numbers using VBA?

  11. 11

    How format number value in php?

  12. 12

    How to remove thousand separaters in number using javascript?

  13. 13

    Format a number to display a comma when larger than a thousand

  14. 14

    How do I get the number of Facebook-like for each picture?

  15. 15

    How to format number 300 to 3.0 in php

  16. 16

    How to convert price format to number only with php?

  17. 17

    How to convert a number to a specific format in PHP?

  18. 18

    how to change long format number in php?

  19. 19

    PHP add thousand separators to number and keep leading zeros

  20. 20

    How to create static CSRF token in php like facebook and Google?

  21. 21

    Angular 2: pipes - How to format a phone number?

  22. 22

    How to format a decimal number to 2 or 3 digits?

  23. 23

    How to generate a file format like this (javascript array??) ?? in PHP

  24. 24

    PHP number format with integer format

  25. 25

    PHP number format with integer format

  26. 26

    asp.net mvc 5 how to set number into long value like 101-2esdf-1212

  27. 27

    How to use Facebook PHP SDK with Yii 2?

  28. 28

    Number format display in php

  29. 29

    PHP - change number format

HotTag

Archive