Shorten Long/Large Numbers using PHP

Language

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:
  1. <!DOCTYPE html>
  2. <html lang="eng">
  3. <head>
  4. <title>PHP Number Abbreviator</title>
  5. </head>
  6. <body>
  7. <?php
  8. function thousandsCurrencyFormat($num) {
  9. $x = round($num);
  10. $x_number_format = number_format($x);
  11. $x_array = explode(',', $x_number_format);
  12. $x_parts = array('k', 'M', 'B', 'T', 'q', 'Q', 's', 'S', 'O', 'N', 'd');
  13. $x_count_parts = count($x_array) - 1; $x_display = $x;
  14. $x_display = $x_array[0] . ((int) $x_array[1][0] !== 0 ? '.' . $x_array[1][0] : '');
  15. $x_display .= $x_parts[$x_count_parts - 1];
  16. return $x_display;
  17. }
  18. ?>
  19. <table cellpadding="15" cellspacing="5" border="1">
  20. <thead>
  21. <tr>
  22. <th>Description</th>
  23. <th>Symbol</th>
  24. <th>Number</th>
  25. <th>Result</th>
  26. </tr>
  27. </thead>
  28. <tbody>
  29. <tr>
  30. <td style="text-align:center;">Trillion</td>
  31. <td style="text-align:center;">T</td>
  32. <td style="text-align:center;">7,300,000,000,000</td>
  33. <td style="text-align:center;"><?php echo thousandsCurrencyFormat(7300000000000)?></td>
  34. </tr>
  35. <tr>
  36. <td style="text-align:center;">Billion</td>
  37. <td style="text-align:center;">B</td>
  38. <td style="text-align:center;">9,000,000,000</td>
  39. <td style="text-align:center;"><?php echo thousandsCurrencyFormat(9000000000)?></td>
  40. </tr>
  41. <tr>
  42. <td style="text-align:center;">Million</td>
  43. <td style="text-align:center;">M</td>
  44. <td style="text-align:center;">12,000,000</td>
  45. <td style="text-align:center;"><?php echo thousandsCurrencyFormat(12000000)?></td>
  46. </tr>
  47. <tr>
  48. <td style="text-align:center;">Thousand</td>
  49. <td style="text-align:center;">K</td>
  50. <td style="text-align:center;">588,000</td>
  51. <td style="text-align:center;"><?php echo thousandsCurrencyFormat(588000)?></td>
  52. </tr>
  53. </tbody>
  54. </table>
  55. </body>
  56. </html>
  57.  
  58.  

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.

FOR YOUR OWN SAFETY, PLEASE:

1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

Add new comment