PHP Human Number Function

December 5, 2010
This is a pretty simplistic function to convert numbers to "Human Numbers". Outside of the function, you can see an example usage which converts:

1
15
23
1234
12345678


To This:
1st
15th
23rd
1,234th
12,345,678th


Download Original
  1. <?PHP
  2. function humanNumber($number=0)
  3. {
  4. if (is_numeric($number)) {
  5. $number = number_format($number);
  6. SWITCH (substr($number,-1)) {
  7. CASE 1:
  8. $number .= "st";
  9. break;
  10. CASE 2:
  11. $number .= "nd";
  12. break;
  13. CASE 3:
  14. $number .= "rd";
  15. break;
  16. DEFAULT:
  17. $number .= "th";
  18. break;
  19. }
  20. }
  21. RETURN $number;
  22. }
  23.  
  24. echo humanNumber(1) . "<br />" . humanNumber(15) . "<br />" . humanNumber(23) . "<br />" . humanNumber(1234) . "<br />" . humanNumber("12345678");


Because this function is so darn easy to make and use, you can use it for whatever you want, just don't copyright it on me (really, is this worth the $275 to do so???).
Name:

No comments yet! Be the first!