Advertisement
bssanchez93

Convert number to its textual name

Sep 9th, 2014
429
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 6.64 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4.  * English Number Converter - Collection of PHP functions to convert a number
  5.  *                            into English text.
  6.  *
  7.  * This exact code is licensed under CC-Wiki on Stackoverflow.
  8.  * http://creativecommons.org/licenses/by-sa/3.0/
  9.  *
  10.  * @link     http://stackoverflow.com/q/277569/367456
  11.  * @question Is there an easy way to convert a number to a word in PHP?
  12.  *
  13.  * This file incorporates work covered by the following copyright and
  14.  * permission notice:
  15.  *
  16.  *   Copyright 2007-2008 Brenton Fletcher. http://bloople.net/num2text
  17.  *   You can use this freely and modify it however you want.
  18.  *
  19.  * Class for POO by kid_goth
  20.  */
  21. class Num2TextEng {
  22.  
  23.     public function convertNumber($number) {
  24.         $output = "";
  25.  
  26.         if (preg_match('/\./', $number)) {
  27.             list($integer, $fraction) = explode(".", (string) $number);
  28.         } else {
  29.             $integer = (string) $number;
  30.             $fraction = NULL;
  31.         }
  32.  
  33.         if ($integer{0} == "-") {
  34.             $output = "negative ";
  35.             $integer = ltrim($integer, "-");
  36.         } else if ($integer{0} == "+") {
  37.             $output = "positive ";
  38.             $integer = ltrim($integer, "+");
  39.         }
  40.  
  41.         if ($integer{0} == "0") {
  42.             $output .= "zero";
  43.         } else {
  44.             $integer = str_pad($integer, 36, "0", STR_PAD_LEFT);
  45.             $group = rtrim(chunk_split($integer, 3, " "), " ");
  46.             $groups = explode(" ", $group);
  47.  
  48.             $groups2 = array();
  49.             foreach ($groups as $g) {
  50.                 $groups2[] = $this->convertThreeDigit($g{0}, $g{1}, $g{2});
  51.             }
  52.  
  53.             for ($z = 0; $z < count($groups2); $z++) {
  54.                 if ($groups2[$z] != "") {
  55.                     $output .= $groups2[$z] . $this->convertGroup(11 - $z) . (
  56.                             $z < 11 && !array_search('', array_slice($groups2, $z + 1, -1)) && $groups2[11] != '' && $groups[11]{0} == '0' ? " and " : ", "
  57.                             );
  58.                 }
  59.             }
  60.  
  61.             $output = rtrim($output, ", ");
  62.         }
  63.  
  64.         if ($fraction > 0) {
  65.             $output .= " point";
  66.             for ($i = 0; $i < strlen($fraction); $i++) {
  67.                 $output .= " " . $this->convertDigit($fraction{$i});
  68.             }
  69.         }
  70.  
  71.         return $output;
  72.     }
  73.  
  74.     private function convertGroup($index) {
  75.         switch ($index) {
  76.             case 11:
  77.                 return " decillion";
  78.             case 10:
  79.                 return " nonillion";
  80.             case 9:
  81.                 return " octillion";
  82.             case 8:
  83.                 return " septillion";
  84.             case 7:
  85.                 return " sextillion";
  86.             case 6:
  87.                 return " quintrillion";
  88.             case 5:
  89.                 return " quadrillion";
  90.             case 4:
  91.                 return " trillion";
  92.             case 3:
  93.                 return " billion";
  94.             case 2:
  95.                 return " million";
  96.             case 1:
  97.                 return " thousand";
  98.             case 0:
  99.                 return "";
  100.         }
  101.     }
  102.  
  103.     private function convertThreeDigit($digit1, $digit2, $digit3) {
  104.         $buffer = "";
  105.  
  106.         if ($digit1 == "0" && $digit2 == "0" && $digit3 == "0") {
  107.             return "";
  108.         }
  109.  
  110.         if ($digit1 != "0") {
  111.             $buffer .= $this->convertDigit($digit1) . " hundred";
  112.             if ($digit2 != "0" || $digit3 != "0") {
  113.                 $buffer .= " and ";
  114.             }
  115.         }
  116.  
  117.         if ($digit2 != "0") {
  118.             $buffer .= $this->convertTwoDigit($digit2, $digit3);
  119.         } else if ($digit3 != "0") {
  120.             $buffer .= $this->convertDigit($digit3);
  121.         }
  122.  
  123.         return $buffer;
  124.     }
  125.  
  126.     private function convertTwoDigit($digit1, $digit2) {
  127.         if ($digit2 == "0") {
  128.             switch ($digit1) {
  129.                 case "1":
  130.                     return "ten";
  131.                 case "2":
  132.                     return "twenty";
  133.                 case "3":
  134.                     return "thirty";
  135.                 case "4":
  136.                     return "forty";
  137.                 case "5":
  138.                     return "fifty";
  139.                 case "6":
  140.                     return "sixty";
  141.                 case "7":
  142.                     return "seventy";
  143.                 case "8":
  144.                     return "eighty";
  145.                 case "9":
  146.                     return "ninety";
  147.             }
  148.         } else if ($digit1 == "1") {
  149.             switch ($digit2) {
  150.                 case "1":
  151.                     return "eleven";
  152.                 case "2":
  153.                     return "twelve";
  154.                 case "3":
  155.                     return "thirteen";
  156.                 case "4":
  157.                     return "fourteen";
  158.                 case "5":
  159.                     return "fifteen";
  160.                 case "6":
  161.                     return "sixteen";
  162.                 case "7":
  163.                     return "seventeen";
  164.                 case "8":
  165.                     return "eighteen";
  166.                 case "9":
  167.                     return "nineteen";
  168.             }
  169.         } else {
  170.             $temp = $this->convertDigit($digit2);
  171.             switch ($digit1) {
  172.                 case "2":
  173.                     return "twenty-$temp";
  174.                 case "3":
  175.                     return "thirty-$temp";
  176.                 case "4":
  177.                     return "forty-$temp";
  178.                 case "5":
  179.                     return "fifty-$temp";
  180.                 case "6":
  181.                     return "sixty-$temp";
  182.                 case "7":
  183.                     return "seventy-$temp";
  184.                 case "8":
  185.                     return "eighty-$temp";
  186.                 case "9":
  187.                     return "ninety-$temp";
  188.             }
  189.         }
  190.     }
  191.  
  192.     private function convertDigit($digit) {
  193.         switch ($digit) {
  194.             case "0":
  195.                 return "zero";
  196.             case "1":
  197.                 return "one";
  198.             case "2":
  199.                 return "two";
  200.             case "3":
  201.                 return "three";
  202.             case "4":
  203.                 return "four";
  204.             case "5":
  205.                 return "five";
  206.             case "6":
  207.                 return "six";
  208.             case "7":
  209.                 return "seven";
  210.             case "8":
  211.                 return "eight";
  212.             case "9":
  213.                 return "nine";
  214.         }
  215.     }
  216.  
  217. }
  218.  
  219. /**
  220.  * Example of use:
  221.  *
  222.  * http://localhost/Num2TextEng.php?n=1000
  223.  * */
  224. $objNum2TextEng = new Num2TextEng();
  225. $_G = filter_input(INPUT_GET, 'n');
  226. echo $objNum2TextEng->convertNumber($_G);
  227.  
  228. // Output: one thousand
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement