Advertisement
dajare

NumbersToWords Helper

Apr 15th, 2013
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.92 KB | None | 0 0
  1. <?
  2. /*
  3. Copyright 2007-2008 Brenton Fletcher. http://bloople.net/num2text
  4. You can use this freely and modify it however you want.
  5.  
  6. Used as "NumbersToWords" helper in Wolf CMS in comments form.
  7. */
  8. function convertNumber($num)
  9. {
  10.    list($num, $dec) = explode(".", $num);
  11.  
  12.    $output = "";
  13.  
  14.    if($num{0} == "-")
  15.    {
  16.       $output = "negative ";
  17.       $num = ltrim($num, "-");
  18.    }
  19.    else if($num{0} == "+")
  20.    {
  21.       $output = "positive ";
  22.       $num = ltrim($num, "+");
  23.    }
  24.    
  25.    if($num{0} == "0")
  26.    {
  27.       $output .= "zero";
  28.    }
  29.    else
  30.    {
  31.       $num = str_pad($num, 36, "0", STR_PAD_LEFT);
  32.       $group = rtrim(chunk_split($num, 3, " "), " ");
  33.       $groups = explode(" ", $group);
  34.  
  35.       $groups2 = array();
  36.       foreach($groups as $g) $groups2[] = convertThreeDigit($g{0}, $g{1}, $g{2});
  37.  
  38.       for($z = 0; $z < count($groups2); $z++)
  39.       {
  40.          if($groups2[$z] != "")
  41.          {
  42.             $output .= $groups2[$z].convertGroup(11 - $z).($z < 11 && !array_search('', array_slice($groups2, $z + 1, -1))
  43.              && $groups2[11] != '' && $groups[11]{0} == '0' ? " and " : ", ");
  44.          }
  45.       }
  46.  
  47.       $output = rtrim($output, ", ");
  48.    }
  49.  
  50.    if($dec > 0)
  51.    {
  52.       $output .= " point";
  53.       for($i = 0; $i < strlen($dec); $i++) $output .= " ".convertDigit($dec{$i});
  54.    }
  55.  
  56.    return $output;
  57. }
  58.  
  59. function convertGroup($index)
  60. {
  61.    switch($index)
  62.    {
  63.       case 11: return " decillion";
  64.       case 10: return " nonillion";
  65.       case 9: return " octillion";
  66.       case 8: return " septillion";
  67.       case 7: return " sextillion";
  68.       case 6: return " quintrillion";
  69.       case 5: return " quadrillion";
  70.       case 4: return " trillion";
  71.       case 3: return " billion";
  72.       case 2: return " million";
  73.       case 1: return " thousand";
  74.       case 0: return "";
  75.    }
  76. }
  77.  
  78. function convertThreeDigit($dig1, $dig2, $dig3)
  79. {
  80.    $output = "";
  81.  
  82.    if($dig1 == "0" && $dig2 == "0" && $dig3 == "0") return "";
  83.  
  84.    if($dig1 != "0")
  85.    {
  86.       $output .= convertDigit($dig1)." hundred";
  87.       if($dig2 != "0" || $dig3 != "0") $output .= " and ";
  88.    }
  89.  
  90.    if($dig2 != "0") $output .= convertTwoDigit($dig2, $dig3);
  91.    else if($dig3 != "0") $output .= convertDigit($dig3);
  92.  
  93.    return $output;
  94. }
  95.  
  96. function convertTwoDigit($dig1, $dig2)
  97. {
  98.    if($dig2 == "0")
  99.    {
  100.       switch($dig1)
  101.       {
  102.          case "1": return "ten";
  103.          case "2": return "twenty";
  104.          case "3": return "thirty";
  105.          case "4": return "forty";
  106.          case "5": return "fifty";
  107.          case "6": return "sixty";
  108.          case "7": return "seventy";
  109.          case "8": return "eighty";
  110.          case "9": return "ninety";
  111.       }
  112.    }
  113.    else if($dig1 == "1")
  114.    {
  115.       switch($dig2)
  116.       {
  117.          case "1": return "eleven";
  118.          case "2": return "twelve";
  119.          case "3": return "thirteen";
  120.          case "4": return "fourteen";
  121.          case "5": return "fifteen";
  122.          case "6": return "sixteen";
  123.          case "7": return "seventeen";
  124.          case "8": return "eighteen";
  125.          case "9": return "nineteen";
  126.       }
  127.    }
  128.    else
  129.    {
  130.       $temp = convertDigit($dig2);
  131.       switch($dig1)
  132.       {
  133.          case "2": return "twenty-$temp";
  134.          case "3": return "thirty-$temp";
  135.          case "4": return "forty-$temp";
  136.          case "5": return "fifty-$temp";
  137.          case "6": return "sixty-$temp";
  138.          case "7": return "seventy-$temp";
  139.          case "8": return "eighty-$temp";
  140.          case "9": return "ninety-$temp";
  141.       }
  142.    }
  143. }
  144.      
  145. function convertDigit($digit)
  146. {
  147.    switch($digit)
  148.    {
  149.       case "0": return "zero";
  150.       case "1": return "one";
  151.       case "2": return "two";
  152.       case "3": return "three";
  153.       case "4": return "four";
  154.       case "5": return "five";
  155.       case "6": return "six";
  156.       case "7": return "seven";
  157.       case "8": return "eight";
  158.       case "9": return "nine";
  159.    }
  160. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement