Foros del Web » Programando para Internet » PHP »

problema funcion numeros a letra

Estas en el tema de problema funcion numeros a letra en el foro de PHP en Foros del Web. @import url("http://static.forosdelweb.com/clientscript/vbulletin_css/geshi.css"); Código PHP: Ver original function centimos ( ) {     global $importe_parcial ;       $importe_parcial = number_format ( $importe_parcial , ...
  #1 (permalink)  
Antiguo 29/04/2010, 07:07
 
Fecha de Ingreso: abril-2010
Mensajes: 143
Antigüedad: 14 años
Puntos: 0
problema funcion numeros a letra

Código PHP:
Ver original
  1. function centimos()
  2. {
  3.     global $importe_parcial;
  4.  
  5.     $importe_parcial = number_format($importe_parcial, 2, ".", "") * 100;
  6.  
  7.     if ($importe_parcial > 0)
  8.         $num_letra = " con ".decena_centimos($importe_parcial);
  9.     else
  10.         $num_letra = "";
  11.  
  12.     return $num_letra;
  13. }
  14.  
  15. function unidad_centimos($numero)
  16. {
  17.     switch ($numero)
  18.     {
  19.         case 9:
  20.         {
  21.             $num_letra = "nueve centavos";
  22.             break;
  23.         }
  24.         case 8:
  25.         {
  26.             $num_letra = "ocho centavos";
  27.             break;
  28.         }
  29.         case 7:
  30.         {
  31.             $num_letra = "siete centavos";
  32.             break;
  33.         }
  34.         case 6:
  35.         {
  36.             $num_letra = "seis centavos";
  37.             break;
  38.         }
  39.         case 5:
  40.         {
  41.             $num_letra = "cinco centavos";
  42.             break;
  43.         }
  44.         case 4:
  45.         {
  46.             $num_letra = "cuatro centavos";
  47.             break;
  48.         }
  49.         case 3:
  50.         {
  51.             $num_letra = "tres centavos";
  52.             break;
  53.         }
  54.         case 2:
  55.         {
  56.             $num_letra = "dos centavos";
  57.             break;
  58.         }
  59.         case 1:
  60.         {
  61.             $num_letra = "un centavo";
  62.             break;
  63.         }
  64.     }
  65.     return $num_letra;
  66. }
  67.  
  68. function decena_centimos($numero)
  69. {
  70.     if ($numero >= 10)
  71.     {
  72.         if ($numero >= 90 && $numero <= 99)
  73.         {
  74.               if ($numero == 90)
  75.                   return "noventa centavos";
  76.               else if ($numero == 91)
  77.                   return "noventa y un centavos";
  78.               else
  79.                   return "noventa y ".unidad_centimos($numero - 90);
  80.         }
  81.         if ($numero >= 80 && $numero <= 89)
  82.         {
  83.             if ($numero == 80)
  84.                 return "ochenta centavos";
  85.             else if ($numero == 81)
  86.                 return "ochenta y un centavos";
  87.             else
  88.                 return "ochenta y ".unidad_centimos($numero - 80);
  89.         }
  90.         if ($numero >= 70 && $numero <= 79)
  91.         {
  92.             if ($numero == 70)
  93.                 return "setenta centavos";
  94.             else if ($numero == 71)
  95.                 return "setenta y un centavos";
  96.             else
  97.                 return "setenta y ".unidad_centimos($numero - 70);
  98.         }
  99.         if ($numero >= 60 && $numero <= 69)
  100.         {
  101.             if ($numero == 60)
  102.                 return "sesenta centavos";
  103.             else if ($numero == 61)
  104.                 return "sesenta y un centavos";
  105.             else
  106.                 return "sesenta y ".unidad_centimos($numero - 60);
  107.         }
  108.         if ($numero >= 50 && $numero <= 59)
  109.         {
  110.             if ($numero == 50)
  111.                 return "cincuenta centavos";
  112.             else if ($numero == 51)
  113.                 return "cincuenta y un centavos";
  114.             else
  115.                 return "cincuenta y ".unidad_centimos($numero - 50);
  116.         }
  117.         if ($numero >= 40 && $numero <= 49)
  118.         {
  119.             if ($numero == 40)
  120.                 return "cuarenta centavos";
  121.             else if ($numero == 41)
  122.                 return "cuarenta y un centavos";
  123.             else
  124.                 return "cuarenta y ".unidad_centimos($numero - 40);
  125.         }
  126.         if ($numero >= 30 && $numero <= 39)
  127.         {
  128.             if ($numero == 30)
  129.                 return "treinta centavos";
  130.             else if ($numero == 91)
  131.                 return "treinta y un centavos";
  132.             else
  133.                 return "treinta y ".unidad_centimos($numero - 30);
  134.         }
  135.         if ($numero >= 20 && $numero <= 29)
  136.         {
  137.             if ($numero == 20)
  138.                 return "veinte centavos";
  139.             else if ($numero == 21)
  140.                 return "veintiun centavos";
  141.             else
  142.                 return "veinti".unidad_centimos($numero - 20);
  143.         }
  144.         if ($numero >= 10 && $numero <= 19)
  145.         {
  146.             if ($numero == 10)
  147.                 return "diez centavos";
  148.             else if ($numero == 11)
  149.                 return "once centavos";
  150.             else if ($numero == 12)
  151.                 return "doce centavos";
  152.             else if ($numero == 13)
  153.                 return "trece centavos";
  154.             else if ($numero == 14)
  155.                 return "catorce centavos";
  156.             else if ($numero == 15)
  157.                 return "quince centavos";
  158.             else if ($numero == 16)
  159.                 return "dieciseis centavos";
  160.             else if ($numero == 17)
  161.                 return "diecisiete centavos";
  162.             else if ($numero == 18)
  163.                 return "dieciocho centavos";
  164.             else if ($numero == 19)
  165.                 return "diecinueve centavos";
  166.         }
  167.     }
  168.     else
  169.         return unidad_centimos($numero);
  170. }
  171.  
  172. function unidad($numero)
  173. {
  174.     switch ($numero)
  175.     {
  176.         case 9:
  177.         {
  178.             $num = "nueve";
  179.             break;
  180.         }
  181.         case 8:
  182.         {
  183.             $num = "ocho";
  184.             break;
  185.         }
  186.         case 7:
  187.         {
  188.             $num = "siete";
  189.             break;
  190.         }
  191.         case 6:
  192.         {
  193.             $num = "seis";
  194.             break;
  195.         }
  196.         case 5:
  197.         {
  198.             $num = "cinco";
  199.             break;
  200.         }
  201.         case 4:
  202.         {
  203.             $num = "cuatro";
  204.             break;
  205.         }
  206.         case 3:
  207.         {
  208.             $num = "tres";
  209.             break;
  210.         }
  211.         case 2:
  212.         {
  213.             $num = "dos";
  214.             break;
  215.         }
  216.         case 1:
  217.         {
  218.             $num = "uno";
  219.             break;
  220.         }
  221.     }
  222.     return $num;
  223. }
  224.  
  225. function decena($numero)
  226. {
  227.     if ($numero >= 90 && $numero <= 99)
  228.     {
  229.         $num_letra = "noventa ";
  230.        
  231.         if ($numero > 90)
  232.             $num_letra = $num_letra."y ".unidad($numero - 90);
  233.     }
  234.     else if ($numero >= 80 && $numero <= 89)
  235.     {
  236.         $num_letra = "ochenta ";
  237.        
  238.         if ($numero > 80)
  239.             $num_letra = $num_letra."y ".unidad($numero - 80);
  240.     }
  241.     else if ($numero >= 70 && $numero <= 79)
  242.     {
  243.             $num_letra = "setenta ";
  244.        
  245.         if ($numero > 70)
  246.             $num_letra = $num_letra."y ".unidad($numero - 70);
  247.     }
  248.     else if ($numero >= 60 && $numero <= 69)
  249.     {
  250.         $num_letra = "sesenta ";
  251.        
  252.         if ($numero > 60)
  253.             $num_letra = $num_letra."y ".unidad($numero - 60);
  254.     }
  255.     else if ($numero >= 50 && $numero <= 59)
  256.     {
  257.         $num_letra = "cincuenta ";
  258.        
  259.         if ($numero > 50)
  260.             $num_letra = $num_letra."y ".unidad($numero - 50);
  261.     }
  262.     else if ($numero >= 40 && $numero <= 49)
  263.     {
  264.         $num_letra = "cuarenta ";
  265.        
  266.         if ($numero > 40)
  267.             $num_letra = $num_letra."y ".unidad($numero - 40);
  268.     }
  269.     else if ($numero >= 30 && $numero <= 39)
  270.     {
  271.         $num_letra = "treinta ";
  272.        
  273.         if ($numero > 30)
  274.             $num_letra = $num_letra."y ".unidad($numero - 30);
ciertos numeros como los q terminan en ",56" no me funcionan...
  #2 (permalink)  
Antiguo 29/04/2010, 07:08
 
Fecha de Ingreso: abril-2010
Mensajes: 143
Antigüedad: 14 años
Puntos: 0
Respuesta: problema funcion numeros a letra

Código PHP:
Ver original
  1. }
  2.     else if ($numero >= 20 && $numero <= 29)
  3.     {
  4.         if ($numero == 20)
  5.             $num_letra = "veinte ";
  6.         else
  7.             $num_letra = "veinti".unidad($numero - 20);
  8.     }
  9.     else if ($numero >= 10 && $numero <= 19)
  10.     {
  11.         switch ($numero)
  12.         {
  13.             case 10:
  14.             {
  15.                 $num_letra = "diez ";
  16.                 break;
  17.             }
  18.             case 11:
  19.             {
  20.                 $num_letra = "once ";
  21.                 break;
  22.             }
  23.             case 12:
  24.             {
  25.                 $num_letra = "doce ";
  26.                 break;
  27.             }
  28.             case 13:
  29.             {
  30.                 $num_letra = "trece ";
  31.                 break;
  32.             }
  33.             case 14:
  34.             {
  35.                 $num_letra = "catorce ";
  36.                 break;
  37.             }
  38.             case 15:
  39.             {
  40.                 $num_letra = "quince ";
  41.                 break;
  42.             }
  43.             case 16:
  44.             {
  45.                 $num_letra = "dieciseis ";
  46.                 break;
  47.             }
  48.             case 17:
  49.             {
  50.                 $num_letra = "diecisiete ";
  51.                 break;
  52.             }
  53.             case 18:
  54.             {
  55.                 $num_letra = "dieciocho ";
  56.                 break;
  57.             }
  58.             case 19:
  59.             {
  60.                 $num_letra = "diecinueve ";
  61.                 break;
  62.             }
  63.         }
  64.     }
  65.     else
  66.         $num_letra = unidad($numero);
  67.  
  68.     return $num_letra;
  69. }
  70.  
  71. function centena($numero)
  72. {
  73.     if ($numero >= 100)
  74.     {
  75.         if ($numero >= 900 & $numero <= 999)
  76.         {
  77.             $num_letra = "novecientos ";
  78.            
  79.             if ($numero > 900)
  80.                 $num_letra = $num_letra.decena($numero - 900);
  81.         }
  82.         else if ($numero >= 800 && $numero <= 899)
  83.         {
  84.             $num_letra = "ochocientos ";
  85.            
  86.             if ($numero > 800)
  87.                 $num_letra = $num_letra.decena($numero - 800);
  88.         }
  89.         else if ($numero >= 700 && $numero <= 799)
  90.         {
  91.             $num_letra = "setecientos ";
  92.            
  93.             if ($numero > 700)
  94.                 $num_letra = $num_letra.decena($numero - 700);
  95.         }
  96.         else if ($numero >= 600 && $numero <= 699)
  97.         {
  98.             $num_letra = "seiscientos ";
  99.            
  100.             if ($numero > 600)
  101.                 $num_letra = $num_letra.decena($numero - 600);
  102.         }
  103.         else if ($numero >= 500 && $numero <= 599)
  104.         {
  105.             $num_letra = "quinientos ";
  106.            
  107.             if ($numero > 500)
  108.                 $num_letra = $num_letra.decena($numero - 500);
  109.         }
  110.         else if ($numero >= 400 && $numero <= 499)
  111.         {
  112.             $num_letra = "cuatrocientos ";
  113.            
  114.             if ($numero > 400)
  115.                 $num_letra = $num_letra.decena($numero - 400);
  116.         }
  117.         else if ($numero >= 300 && $numero <= 399)
  118.         {
  119.             $num_letra = "trescientos ";
  120.            
  121.             if ($numero > 300)
  122.                 $num_letra = $num_letra.decena($numero - 300);
  123.         }
  124.         else if ($numero >= 200 && $numero <= 299)
  125.         {
  126.             $num_letra = "doscientos ";
  127.            
  128.             if ($numero > 200)
  129.                 $num_letra = $num_letra.decena($numero - 200);
  130.         }
  131.         else if ($numero >= 100 && $numero <= 199)
  132.         {
  133.             if ($numero == 100)
  134.                 $num_letra = "cien ";
  135.             else
  136.                 $num_letra = "ciento ".decena($numero - 100);
  137.         }
  138.     }
  139.     else
  140.         $num_letra = decena($numero);
  141.    
  142.     return $num_letra;
  143. }
  144.  
  145. function cien()
  146. {
  147.     global $importe_parcial;
  148.    
  149.     $parcial = 0; $car = 0;
  150.    
  151.     while (substr($importe_parcial, 0, 1) == 0)
  152.         $importe_parcial = substr($importe_parcial, 1, strlen($importe_parcial) - 1);
  153.    
  154.     if ($importe_parcial >= 1 && $importe_parcial <= 9.99)
  155.         $car = 1;
  156.     else if ($importe_parcial >= 10 && $importe_parcial <= 99.99)
  157.         $car = 2;
  158.     else if ($importe_parcial >= 100 && $importe_parcial <= 999.99)
  159.         $car = 3;
  160.    
  161.     $parcial = substr($importe_parcial, 0, $car);
  162.     $importe_parcial = substr($importe_parcial, $car);
  163.    
  164.     $num_letra = centena($parcial).centimos();
  165.    
  166.     return $num_letra;
  167. }
  168.  
  169. function cien_mil()
  170. {
  171.     global $importe_parcial;
  172.    
  173.     $parcial = 0; $car = 0;
  174.    
  175.     while (substr($importe_parcial, 0, 1) == 0)
  176.         $importe_parcial = substr($importe_parcial, 1, strlen($importe_parcial) - 1);
  177.    
  178.     if ($importe_parcial >= 1000 && $importe_parcial <= 9999.99)
  179.         $car = 1;
  180.     else if ($importe_parcial >= 10000 && $importe_parcial <= 99999.99)
  181.         $car = 2;
  182.     else if ($importe_parcial >= 100000 && $importe_parcial <= 999999.99)
  183.         $car = 3;
  184.    
  185.     $parcial = substr($importe_parcial, 0, $car);
  186.     $importe_parcial = substr($importe_parcial, $car);
  187.    
  188.     if ($parcial > 0)
  189.     {
  190.         if ($parcial == 1)
  191.             $num_letra = "mil ";
  192.         else
  193.             $num_letra = centena($parcial)." mil ";
  194.     }
  195.    
  196.     return $num_letra;
  197. }
  198.  
  199.  
  200. function millon()
  201. {
  202.     global $importe_parcial;
  203.    
  204.     $parcial = 0; $car = 0;
  205.    
  206.     while (substr($importe_parcial, 0, 1) == 0)
  207.         $importe_parcial = substr($importe_parcial, 1, strlen($importe_parcial) - 1);
  208.    
  209.     if ($importe_parcial >= 1000000 && $importe_parcial <= 9999999.99)
  210.         $car = 1;
  211.     else if ($importe_parcial >= 10000000 && $importe_parcial <= 99999999.99)
  212.         $car = 2;
  213.     else if ($importe_parcial >= 100000000 && $importe_parcial <= 999999999.99)
  214.         $car = 3;
  215.    
  216.     $parcial = substr($importe_parcial, 0, $car);
  217.     $importe_parcial = substr($importe_parcial, $car);
  218.    
  219.     if ($parcial == 1)
  220.         $num_letras = "un millón ";
  221.     else
  222.         $num_letras = centena($parcial)." millones ";
  223.    
  224.     return $num_letras;
  225. }
  226.  
  227. function convertir_a_letras($numero)
  228. {
  229.     global $importe_parcial;
  230.    
  231.     $importe_parcial = $numero;
  232.    
  233.     if ($numero < 1000000000)
  234.     {
  235.         if ($numero >= 1000000 && $numero <= 999999999.99)
  236.             $num_letras = millon().cien_mil().cien();
  237.         else if ($numero >= 1000 && $numero <= 999999.99)
  238.             $num_letras = cien_mil().cien();
  239.         else if ($numero >= 1 && $numero <= 999.99)
  240.             $num_letras = cien();
  241.         else if ($numero >= 0.01 && $numero <= 0.99)
  242.         {
  243.             if ($numero == 0.01)
  244.                 $num_letras = "un centavo";
  245.             else
  246.                 $num_letras = convertir_a_letras(($numero * 100)."/100")." centavos";
  247.         }
  248.     }
  249.     return $num_letras;
  250. }
la parte q faltaba de la funcion...
  #3 (permalink)  
Antiguo 29/04/2010, 07:16
Colaborador
 
Fecha de Ingreso: octubre-2009
Ubicación: Tokyo - Japan !
Mensajes: 3.867
Antigüedad: 14 años, 6 meses
Puntos: 334
Respuesta: problema funcion numeros a letra

mmm en realidad seria mas facil trabajar con arrays no crees?
Código PHP:
Ver original
  1. $str_number = array("cero", "uno", "dos", "tres", "cuatro", "cinco", "seis", "siete", "ocho", "nueve", "diez");
  2.  
  3. $numero = 5;
  4.  
  5. echo $str_number[$numero]." centavos";
__________________
More about me...
~ @rhyudek1
~ Github
  #4 (permalink)  
Antiguo 29/04/2010, 07:26
 
Fecha de Ingreso: abril-2010
Mensajes: 143
Antigüedad: 14 años
Puntos: 0
Respuesta: problema funcion numeros a letra

pero como seria el condicional de ese array?

Etiquetas: funcion, letra, numeros
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 10:39.