Advertisement
bssanchez93

Convertir Número a (su forma escrita) Texto

Sep 9th, 2014
521
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 8.43 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4.  * Tomado de:
  5.  *  Copyright 2007-2008 Brenton Fletcher. http://bloople.net/num2text
  6.  *  @link     http://stackoverflow.com/q/277569/367456
  7.  *  @question Is there an easy way to convert a number to a word in PHP?
  8.  *
  9.  * Bajo la licencia de CC-Wiki on Stackoverflow.
  10.  * http://creativecommons.org/licenses/by-sa/3.0/
  11.  *
  12.  *
  13.  * Modificado por kid_goth para uso en regiones de habla española
  14.  * En donde se 1.000.000.000 es igual a Mil millones y no a un
  15.  * billon como se usa en las regiones de habla inglesa. Ademas de agregar
  16.  * POO a la programacion.
  17.  *
  18.  *  @contact Twitter: @_kid_goth
  19.  */
  20. class Num2Text {
  21.  
  22.     public function convertNumber($number) {
  23.         $output = "";
  24.  
  25.         if (preg_match('/\./', $number)) {
  26.             list($integer, $fraction) = explode(".", (string) $number);
  27.         } else {
  28.             $integer = (string) $number;
  29.             $fraction = NULL;
  30.         }
  31.  
  32.         if ($integer{0} == "-") {
  33.             $output = "negativo ";
  34.             $integer = ltrim($integer, "-");
  35.         } else if ($integer{0} == "+") {
  36.             $output = "positivo ";
  37.             $integer = ltrim($integer, "+");
  38.         }
  39.  
  40.         if ($integer{0} == "0") {
  41.             $output .= "cero";
  42.         } else {
  43.             $integer = str_pad($integer, 36, "0", STR_PAD_LEFT);
  44.             $group = rtrim(chunk_split($integer, 3, " "), " ");
  45.             $groups = explode(" ", $group);
  46.  
  47.             $groups2 = array();
  48.             foreach ($groups as $g) {
  49.                 $groups2[] = $this->convertThreeDigit($g{0}, $g{1}, $g{2});
  50.             }
  51.  
  52.             for ($z = 0; $z < count($groups2); $z++) {
  53.                 if ($groups2[$z] != "") {
  54.                     $rtnGroup = $this->convertGroup(11 - $z, $groups2[$z]);
  55.                     $output = preg_replace("/" . $rtnGroup . " /", "", $output);
  56.                     $output .= $groups2[$z] . $rtnGroup . (
  57.                             $z < 11 && !array_search('', array_slice($groups2, $z + 1, -1)) && $groups2[11] != '' && $groups[11]{0} == '0' ? " " : " "
  58. //$z < 11 && !array_search('', array_slice($groups2, $z + 1, -1)) && $groups2[11] != '' && $groups[11]{0} == '0' ? " and " : ", "
  59.                             );
  60.                 }
  61.             }
  62.  
  63.             $output = rtrim($output, ", ");
  64.         }
  65.  
  66.         if ($fraction > 0) {
  67.             $output .= " punto";
  68.             for ($i = 0; $i < strlen($fraction); $i++) {
  69.                 $output .= " " . $this->convertDigit($fraction{$i});
  70.             }
  71.         }
  72.  
  73.         return preg_replace('/un mil\b/', 'mil', $output);
  74.     }
  75.  
  76.     private function convertGroup($index, $valor = '') {
  77.         switch ($index) {
  78.             case 12:
  79.                 if ($valor != 'un') {
  80.                     return " sextillones";
  81.                 } else {
  82.                     return " sextillon";
  83.                 }
  84.             case 11:
  85.                 return " mil quintillones";
  86.             case 10:
  87.                 if ($valor != 'un') {
  88.                     return " quintillones";
  89.                 } else {
  90.                     return " quintillon";
  91.                 }
  92.             case 9:
  93.                 return " mil cuadrillones";
  94.             case 8:
  95.                 if ($valor != 'un') {
  96.                     return " cuadrillones";
  97.                 } else {
  98.                     return " cuadrillon";
  99.                 }
  100.             case 7:
  101.                 return " mil trillones";
  102.             case 6:
  103.                 if ($valor != 'un') {
  104.                     return " trillones";
  105.                 } else {
  106.                     return " trillon";
  107.                 }
  108.             case 5:
  109.                 return " mil billones";
  110.             case 4:
  111.                 if ($valor != 'un') {
  112.                     return " billones";
  113.                 } else {
  114.                     return " billon";
  115.                 }
  116.             case 3:
  117.                 return " mil millones";
  118.             case 2:
  119.                 if ($valor != 'un') {
  120.                     return " millones";
  121.                 } else {
  122.                     return " millon";
  123.                 }
  124.             case 1:
  125.                 return " mil";
  126.             case 0:
  127.                 return "";
  128.         }
  129.     }
  130.  
  131.     private function convertThreeDigit($digit1, $digit2, $digit3) {
  132.         $buffer = "";
  133.  
  134.         if ($digit1 == "0" && $digit2 == "0" && $digit3 == "0") {
  135.             return "";
  136.         }
  137.  
  138.         if ($digit1 != "0") {
  139.             $buffer .= $this->convertCientos($digit1);
  140.             if ($digit2 != "0" || $digit3 != "0") {
  141.                 $buffer .= " ";
  142.             }
  143.         }
  144.  
  145.         if ($digit2 != "0") {
  146.             $buffer .= $this->convertTwoDigit($digit2, $digit3);
  147.         } else if ($digit3 != "0") {
  148.             $buffer .= $this->convertDigit($digit3);
  149.         }
  150.  
  151.         return $buffer;
  152.     }
  153.  
  154.     private function convertTwoDigit($digit1, $digit2) {
  155.         if ($digit2 == "0") {
  156.             switch ($digit1) {
  157.                 case "1":
  158.                     return "diez";
  159.                 case "2":
  160.                     return "veinte";
  161.                 case "3":
  162.                     return "treinta";
  163.                 case "4":
  164.                     return "cuarenta";
  165.                 case "5":
  166.                     return "cincuenta";
  167.                 case "6":
  168.                     return "sesenta";
  169.                 case "7":
  170.                     return "setenta";
  171.                 case "8":
  172.                     return "ochenta";
  173.                 case "9":
  174.                     return "noventa";
  175.             }
  176.         } else if ($digit1 == "1") {
  177.             switch ($digit2) {
  178.                 case "1":
  179.                     return "once";
  180.                 case "2":
  181.                     return "doce";
  182.                 case "3":
  183.                     return "trece";
  184.                 case "4":
  185.                     return "catorce";
  186.                 case "5":
  187.                     return "quince";
  188.                 case "6":
  189.                     return "diez y seis";
  190.                 case "7":
  191.                     return "diez y siete";
  192.                 case "8":
  193.                     return "diez y ocho";
  194.                 case "9":
  195.                     return "diez y nueve";
  196.             }
  197.         } else {
  198.             $temp = $this->convertDigit($digit2);
  199.             switch ($digit1) {
  200.                 case "2":
  201.                     return "veinti$temp";
  202.                 case "3":
  203.                     return "treinta y $temp";
  204.                 case "4":
  205.                     return "cuarenta y $temp";
  206.                 case "5":
  207.                     return "cincuenta y $temp";
  208.                 case "6":
  209.                     return "sesenta y $temp";
  210.                 case "7":
  211.                     return "setenta y $temp";
  212.                 case "8":
  213.                     return "ochenta y $temp";
  214.                 case "9":
  215.                     return "noventa y $temp";
  216.             }
  217.         }
  218.     }
  219.  
  220.     private function convertCientos($digit) {
  221.         switch ($digit) {
  222.             case "0":
  223.                 return "cero";
  224.             case "1":
  225.                 return "ciento";
  226.             case "2":
  227.                 return "docientos";
  228.             case "3":
  229.                 return "trecientos";
  230.             case "4":
  231.                 return "cuatrocientos";
  232.             case "5":
  233.                 return "quinientos";
  234.             case "6":
  235.                 return "seicientos";
  236.             case "7":
  237.                 return "setecientos";
  238.             case "8":
  239.                 return "ochocientos";
  240.             case "9":
  241.                 return "novecientos";
  242.         }
  243.     }
  244.  
  245.     private function convertDigit($digit) {
  246.         switch ($digit) {
  247.             case "0":
  248.                 return "cero";
  249.             case "1":
  250.                 return "un";
  251.             case "2":
  252.                 return "dos";
  253.             case "3":
  254.                 return "tres";
  255.             case "4":
  256.                 return "cuatro";
  257.             case "5":
  258.                 return "cinco";
  259.             case "6":
  260.                 return "seis";
  261.             case "7":
  262.                 return "siete";
  263.             case "8":
  264.                 return "ocho";
  265.             case "9":
  266.                 return "nueve";
  267.         }
  268.     }
  269.  
  270. }
  271.  
  272. /**
  273.  * Ejemplo de Uso:
  274.  *
  275.  * http://localhost/Num2Text.php?n=1000
  276. **/
  277.  
  278. $objNum2Text = new Num2Text();
  279. $_G = filter_input(INPUT_GET, 'n');
  280. echo $objNum2Text->convertNumber($_G);
  281.  
  282. // Salida: Mil
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement