Shorten Long/Large Numbers PHP Script


Shorten Long/Large Numbers PHP Script

Good Day. In this project we are going learn how to shorten long numbers to K/ M/ B/ T/q/Q/s/S/O/N/d.
This function abbreviates a large number it display numbers like 3.5K and 4.2M.

 

thousandsCurrencyFormat(1000) - 1k (One Thousand)
thousandsCurrencyFormat(1000000) - 1M (One Million)
thousandsCurrencyFormat(1000000000) - 1B (One Billion)
thousandsCurrencyFormat(1000000000000) - 1T (One Trillion)
thousandsCurrencyFormat(1000000000000000) - 1q (One Quadrillion)
thousandsCurrencyFormat(1000000000000000000) - 1Q (One Quintillion)
thousandsCurrencyFormat(1000000000000000000000) - 1s (One Sextillion)
thousandsCurrencyFormat(1000000000000000000000000) - 1S (One Septillion)
thousandsCurrencyFormat(1000000000000000000000000000) - 1O (One Octillion)
thousandsCurrencyFormat(1000000000000000000000000000000) - 1N (One Nonillion)
thousandsCurrencyFormat(1000000000000000000000000000000000) - 1d (One Decillion)

 

Complete Code:

 

 <!DOCTYPE html>                                    
<html lang="eng">
      <head>
            <title>PHP Number Abbreviator</title>
      </head>
      <body>
                  <?php  
                        function thousandsCurrencyFormat($num) {
                        $x = round($num);
                        $x_number_format = number_format($x);
                        $x_array = explode(',', $x_number_format);
                        $x_parts = array('k', 'M', 'B', 'T', 'q', 'Q', 's', 'S', 'O', 'N', 'd');
                        $x_count_parts = count($x_array) - 1;                              $x_display = $x;
                        $x_display = $x_array[0] . ((int) $x_array[1][0] !== 0 ? '.' . $x_array[1][0] : '');
                        $x_display .= $x_parts[$x_count_parts - 1];
                        return $x_display;
                        }
                  ?>
                  <table cellpadding="15" cellspacing="5" border="1">
                        <thead>
                              <tr>
                                    <th>Description</th>
                                    <th>Symbol</th>
                                    <th>Number</th>
                                    <th>Result</th>
                              </tr>
                        </thead>
                        <tbody>
                              <tr>
                                    <td style="text-align:center;">Trillion</td>
                                    <td style="text-align:center;">T</td>
                                    <td style="text-align:center;">7,300,000,000,000</td>
                                    <td style="text-align:center;"><?php echo thousandsCurrencyFormat(7300000000000)?></td>
                              </tr>
                        <tr>
                                    <td style="text-align:center;">Billion</td>
                                    <td style="text-align:center;">B</td>
                                    <td style="text-align:center;">9,000,000,000</td>
                                    <td style="text-align:center;"><?php echo thousandsCurrencyFormat(9000000000)?></td>
                              </tr>
                        <tr>
                                    <td style="text-align:center;">Million</td>
                                    <td style="text-align:center;">M</td>
                                    <td style="text-align:center;">12,000,000</td>
                                    <td style="text-align:center;"><?php echo thousandsCurrencyFormat(12000000)?></td>
                              </tr>
                        <tr>
                                    <td style="text-align:center;">Thousand</td>
                                    <td style="text-align:center;">K</td>
                                    <td style="text-align:center;">588,000</td>
                                    <td style="text-align:center;"><?php echo thousandsCurrencyFormat(588000)?></td>
                              </tr>
                        </tbody>
                  </table>
      </body>
</html>

 

 

Read Me

 

Have any Question or Comment?

Leave a Reply

Your email address will not be published. Required fields are marked *