elhacker.net cabecera Bienvenido(a), Visitante. Por favor Ingresar o Registrarse
¿Perdiste tu email de activación?.

 

 


Tema destacado: Únete al Grupo Steam elhacker.NET


+  Foro de elhacker.net
|-+  Programación
| |-+  Desarrollo Web
| | |-+  PHP (Moderador: #!drvy)
| | | |-+  Problema con funcion convierte numero en letra
0 Usuarios y 1 Visitante están viendo este tema.
Páginas: [1] Ir Abajo Respuesta Imprimir
Autor Tema: Problema con funcion convierte numero en letra  (Leído 4,243 veces)
maxwellnewage

Desconectado Desconectado

Mensajes: 245


Ver Perfil
Problema con funcion convierte numero en letra
« en: 22 Abril 2010, 15:51 pm »

este es el code
Código:
<?php 
function num2letras($num, $fem = true, $dec = true) {
   $matuni[2]  = "dos";
   $matuni[3]  = "tres";
   $matuni[4]  = "cuatro";
   $matuni[5]  = "cinco";
   $matuni[6]  = "seis";
   $matuni[7]  = "siete";
   $matuni[8]  = "ocho";
   $matuni[9]  = "nueve";
   $matuni[10] = "diez";
   $matuni[11] = "once";
   $matuni[12] = "doce";
   $matuni[13] = "trece";
   $matuni[14] = "catorce";
   $matuni[15] = "quince";
   $matuni[16] = "dieciseis";
   $matuni[17] = "diecisiete";
   $matuni[18] = "dieciocho";
   $matuni[19] = "diecinueve";
   $matuni[20] = "veinte";
   $matunisub[2] = "dos";
   $matunisub[3] = "tres";
   $matunisub[4] = "cuatro";
   $matunisub[5] = "quin";
   $matunisub[6] = "seis";
   $matunisub[7] = "sete";
   $matunisub[8] = "ocho";
   $matunisub[9] = "nove";

   $matdec[2] = "veint";
   $matdec[3] = "treinta";
   $matdec[4] = "cuarenta";
   $matdec[5] = "cincuenta";
   $matdec[6] = "sesenta";
   $matdec[7] = "setenta";
   $matdec[8] = "ochenta";
   $matdec[9] = "noventa";
   $matsub[3]  = 'mill';
   $matsub[5]  = 'bill';
   $matsub[7]  = 'mill';
   $matsub[9]  = 'trill';
   $matsub[11] = 'mill';
   $matsub[13] = 'bill';
   $matsub[15] = 'mill';
   $matmil[4]  = 'millones';
   $matmil[6]  = 'billones';
   $matmil[7]  = 'de billones';
   $matmil[8]  = 'millones de billones';
   $matmil[10] = 'trillones';
   $matmil[11] = 'de trillones';
   $matmil[12] = 'millones de trillones';
   $matmil[13] = 'de trillones';
   $matmil[14] = 'billones de trillones';
   $matmil[15] = 'de billones de trillones';
   $matmil[16] = 'millones de billones de trillones';

   $num = trim((string)@$num);
   if ($num[0] == '-') {
      $neg = 'menos ';
      $num = substr($num, 1);
   }else
      $neg = '';
   while ($num[0] == '0') $num = substr($num, 1);
   if ($num[0] < '1' or $num[0] > 9) $num = '0' . $num;
   $zeros = true;
   $punt = false;
   $ent = '';
   $fra = '';
   for ($c = 0; $c < strlen($num); $c++) {
      $n = $num[$c];
      if (! (strpos(".,'''", $n) === false)) {
         if ($punt) break;
         else{
            $punt = true;
            continue;
         }

      }elseif (! (strpos('0123456789', $n) === false)) {
         if ($punt) {
            if ($n != '0') $zeros = false;
            $fra .= $n;
         }else

            $ent .= $n;
      }else

         break;

   }
   $ent = '     ' . $ent;
   if ($dec and $fra and ! $zeros) {
      $fin = ' coma';
      for ($n = 0; $n < strlen($fra); $n++) {
         if (($s = $fra[$n]) == '0')
            $fin .= ' cero';
         elseif ($s == '1')
            $fin .= $fem ? ' una' : ' un';
         else
            $fin .= ' ' . $matuni[$s];
      }
   }else
      $fin = '';
   if ((int)$ent === 0) return 'Cero ' . $fin;
   $tex = '';
   $sub = 0;
   $mils = 0;
   $neutro = false;
   while ( ($num = substr($ent, -3)) != '   ') {
      $ent = substr($ent, 0, -3);
      if (++$sub < 3 and $fem) {
         $matuni[1] = 'una';
         $subcent = 'as';
      }else{
         $matuni[1] = $neutro ? 'un' : 'uno';
         $subcent = 'os';
      }
      $t = '';
      $n2 = substr($num, 1);
      if ($n2 == '00') {
      }elseif ($n2 < 21)
         $t = ' ' . $matuni[(int)$n2];
      elseif ($n2 < 30) {
         $n3 = $num[2];
         if ($n3 != 0) $t = 'i' . $matuni[$n3];
         $n2 = $num[1];
         $t = ' ' . $matdec[$n2] . $t;
      }else{
         $n3 = $num[2];
         if ($n3 != 0) $t = ' y ' . $matuni[$n3];
         $n2 = $num[1];
         $t = ' ' . $matdec[$n2] . $t;
      }
      $n = $num[0];
      if ($n == 1) {
         $t = ' ciento' . $t;
      }elseif ($n == 5){
         $t = ' ' . $matunisub[$n] . 'ient' . $subcent . $t;
      }elseif ($n != 0){
         $t = ' ' . $matunisub[$n] . 'cient' . $subcent . $t;
      }
      if ($sub == 1) {
      }elseif (! isset($matsub[$sub])) {
         if ($num == 1) {
            $t = ' mil';
         }elseif ($num > 1){
            $t .= ' mil';
         }
      }elseif ($num == 1) {
         $t .= ' ' . $matsub[$sub] . '?n';
      }elseif ($num > 1){
         $t .= ' ' . $matsub[$sub] . 'ones';
      }   
      if ($num == '000') $mils ++;
      elseif ($mils != 0) {
         if (isset($matmil[$sub])) $t .= ' ' . $matmil[$sub];
         $mils = 0;
      }
      $neutro = true;
      $tex = $t . $tex;
   }
   $tex = $neg . substr($tex, 1) . $fin;
   return ucfirst($tex);
}

?>
su declaracion:
Código:
num2letras('numero')
Mi problema es que por ejemplo 4,8 lo lee como "cuatro coma ocho" y me piden que se lea como "cuatro coma ochenta". Se que la funcion va a tomar "cuatro coma cero ocho" en ese caso, pero asi me lo piden...


« Última modificación: 22 Abril 2010, 16:02 pm por maxwellnewage » En línea

raul338


Desconectado Desconectado

Mensajes: 2.633


La sonrisa es la mejor forma de afrontar las cosas


Ver Perfil WWW
Re: Problema con funcion convierte numero en letra
« Respuesta #1 en: 22 Abril 2010, 16:20 pm »

Entonces "parte la funcion", separa el string por coma y convierte las 2 partes en numeros (cortando la parte decimal a dos digitos)

ej:
124,502

partes por la coma y tienes: 124 y 502

conviertes 124 a letras: Ciento veinte y cuatro (:| corrijanme porque recien me despierto xD)

y a 502 lo recortas a 2 caracteres (te fijas si redondeas): 50
lo pasas a numeros: Cincuenta

y lo unes por "coma": Ciento veinte y cuatro coma cincuenta

;-) espero que te sirva


En línea

maxwellnewage

Desconectado Desconectado

Mensajes: 245


Ver Perfil
Re: Problema con funcion convierte numero en letra
« Respuesta #2 en: 22 Abril 2010, 16:29 pm »

podes ser mas grafico? xD
no me llevo bien con las funciones -.-
En línea

maxwellnewage

Desconectado Desconectado

Mensajes: 245


Ver Perfil
Re: Problema con funcion convierte numero en letra
« Respuesta #3 en: 22 Abril 2010, 16:39 pm »

consegui otra funcion...pero 3.28 me tira error
Código:
Notice: Undefined variable: num_letra in C:\wamp\www\prueba\decimal.php  on line 68
tres con veinti
la funcion
Código:
<?php
// FUNCIONES DE CONVERSION DE NUMEROS A LETRAS.

function centimos()
{
global $importe_parcial;

$importe_parcial = number_format($importe_parcial, 2, ".", "") * 100;

if ($importe_parcial > 0)
$num_letra = " con ".decena_centimos($importe_parcial);
else
$num_letra = "";

return $num_letra;
}

function unidad_centimos($numero)
{
switch ($numero)
{
case 9:
{
$num_letra = "nueve centavos";
break;
}
case 8:
{
$num_letra = "ocho centavos";
break;
}
case 7:
{
$num_letra = "siete centavos";
break;
}
case 6:
{
$num_letra = "seis centavos";
break;
}
case 5:
{
$num_letra = "cinco centavos";
break;
}
case 4:
{
$num_letra = "cuatro centavos";
break;
}
case 3:
{
$num_letra = "tres centavos";
break;
}
case 2:
{
$num_letra = "dos centavos";
break;
}
case 1:
{
$num_letra = "un centavo";
break;
}
}
return $num_letra;
}

function decena_centimos($numero)
{
if ($numero >= 10)
{
if ($numero >= 90 && $numero <= 99)
{
  if ($numero == 90)
  return "noventa centavos";
  else if ($numero == 91)
  return "noventa y un centavos";
  else
  return "noventa y ".unidad_centimos($numero - 90);
}
if ($numero >= 80 && $numero <= 89)
{
if ($numero == 80)
return "ochenta centavos";
else if ($numero == 81)
return "ochenta y un centavos";
else
return "ochenta y ".unidad_centimos($numero - 80);
}
if ($numero >= 70 && $numero <= 79)
{
if ($numero == 70)
return "setenta centavos";
else if ($numero == 71)
return "setenta y un centavos";
else
return "setenta y ".unidad_centimos($numero - 70);
}
if ($numero >= 60 && $numero <= 69)
{
if ($numero == 60)
return "sesenta centavos";
else if ($numero == 61)
return "sesenta y un centavos";
else
return "sesenta y ".unidad_centimos($numero - 60);
}
if ($numero >= 50 && $numero <= 59)
{
if ($numero == 50)
return "cincuenta centavos";
else if ($numero == 51)
return "cincuenta y un centavos";
else
return "cincuenta y ".unidad_centimos($numero - 50);
}
if ($numero >= 40 && $numero <= 49)
{
if ($numero == 40)
return "cuarenta centavos";
else if ($numero == 41)
return "cuarenta y un centavos";
else
return "cuarenta y ".unidad_centimos($numero - 40);
}
if ($numero >= 30 && $numero <= 39)
{
if ($numero == 30)
return "treinta centavos";
else if ($numero == 91)
return "treinta y un centavos";
else
return "treinta y ".unidad_centimos($numero - 30);
}
if ($numero >= 20 && $numero <= 29)
{
if ($numero == 20)
return "veinte centavos";
else if ($numero == 21)
return "veintiun centavos";
else
return "veinti".unidad_centimos($numero - 20);
}
if ($numero >= 10 && $numero <= 19)
{
if ($numero == 10)
return "diez centavos";
else if ($numero == 11)
return "once centavos";
else if ($numero == 11)
return "doce centavos";
else if ($numero == 11)
return "trece centavos";
else if ($numero == 11)
return "catorce centavos";
else if ($numero == 11)
return "quince centavos";
else if ($numero == 11)
return "dieciseis centavos";
else if ($numero == 11)
return "diecisiete centavos";
else if ($numero == 11)
return "dieciocho centavos";
else if ($numero == 11)
return "diecinueve centavos";
}
}
else
return unidad_centimos($numero);
}

function unidad($numero)
{
switch ($numero)
{
case 9:
{
$num = "nueve";
break;
}
case 8:
{
$num = "ocho";
break;
}
case 7:
{
$num = "siete";
break;
}
case 6:
{
$num = "seis";
break;
}
case 5:
{
$num = "cinco";
break;
}
case 4:
{
$num = "cuatro";
break;
}
case 3:
{
$num = "tres";
break;
}
case 2:
{
$num = "dos";
break;
}
case 1:
{
$num = "uno";
break;
}
}
return $num;
}

function decena($numero)
{
if ($numero >= 90 && $numero <= 99)
{
$num_letra = "noventa ";

if ($numero > 90)
$num_letra = $num_letra."y ".unidad($numero - 90);
}
else if ($numero >= 80 && $numero <= 89)
{
$num_letra = "ochenta ";

if ($numero > 80)
$num_letra = $num_letra."y ".unidad($numero - 80);
}
else if ($numero >= 70 && $numero <= 79)
{
$num_letra = "setenta ";

if ($numero > 70)
$num_letra = $num_letra."y ".unidad($numero - 70);
}
else if ($numero >= 60 && $numero <= 69)
{
$num_letra = "sesenta ";

if ($numero > 60)
$num_letra = $num_letra."y ".unidad($numero - 60);
}
else if ($numero >= 50 && $numero <= 59)
{
$num_letra = "cincuenta ";

if ($numero > 50)
$num_letra = $num_letra."y ".unidad($numero - 50);
}
else if ($numero >= 40 && $numero <= 49)
{
$num_letra = "cuarenta ";

if ($numero > 40)
$num_letra = $num_letra."y ".unidad($numero - 40);
}
else if ($numero >= 30 && $numero <= 39)
{
$num_letra = "treinta ";

if ($numero > 30)
$num_letra = $num_letra."y ".unidad($numero - 30);
}
else if ($numero >= 20 && $numero <= 29)
{
if ($numero == 20)
$num_letra = "veinte ";
else
$num_letra = "veinti".unidad($numero - 20);
}
else if ($numero >= 10 && $numero <= 19)
{
switch ($numero)
{
case 10:
{
$num_letra = "diez ";
break;
}
case 11:
{
$num_letra = "once ";
break;
}
case 12:
{
$num_letra = "doce ";
break;
}
case 13:
{
$num_letra = "trece ";
break;
}
case 14:
{
$num_letra = "catorce ";
break;
}
case 15:
{
$num_letra = "quince ";
break;
}
case 16:
{
$num_letra = "dieciseis ";
break;
}
case 17:
{
$num_letra = "diecisiete ";
break;
}
case 18:
{
$num_letra = "dieciocho ";
break;
}
case 19:
{
$num_letra = "diecinueve ";
break;
}
}
}
else
$num_letra = unidad($numero);

return $num_letra;
}

function centena($numero)
{
if ($numero >= 100)
{
if ($numero >= 900 & $numero <= 999)
{
$num_letra = "novecientos ";

if ($numero > 900)
$num_letra = $num_letra.decena($numero - 900);
}
else if ($numero >= 800 && $numero <= 899)
{
$num_letra = "ochocientos ";

if ($numero > 800)
$num_letra = $num_letra.decena($numero - 800);
}
else if ($numero >= 700 && $numero <= 799)
{
$num_letra = "setecientos ";

if ($numero > 700)
$num_letra = $num_letra.decena($numero - 700);
}
else if ($numero >= 600 && $numero <= 699)
{
$num_letra = "seiscientos ";

if ($numero > 600)
$num_letra = $num_letra.decena($numero - 600);
}
else if ($numero >= 500 && $numero <= 599)
{
$num_letra = "quinientos ";

if ($numero > 500)
$num_letra = $num_letra.decena($numero - 500);
}
else if ($numero >= 400 && $numero <= 499)
{
$num_letra = "cuatrocientos ";

if ($numero > 400)
$num_letra = $num_letra.decena($numero - 400);
}
else if ($numero >= 300 && $numero <= 399)
{
$num_letra = "trescientos ";

if ($numero > 300)
$num_letra = $num_letra.decena($numero - 300);
}
else if ($numero >= 200 && $numero <= 299)
{
$num_letra = "doscientos ";

if ($numero > 200)
$num_letra = $num_letra.decena($numero - 200);
}
else if ($numero >= 100 && $numero <= 199)
{
if ($numero == 100)
$num_letra = "cien ";
else
$num_letra = "ciento ".decena($numero - 100);
}
}
else
$num_letra = decena($numero);

return $num_letra;
}

function cien()
{
global $importe_parcial;

$parcial = 0; $car = 0;

while (substr($importe_parcial, 0, 1) == 0)
$importe_parcial = substr($importe_parcial, 1, strlen($importe_parcial) - 1);

if ($importe_parcial >= 1 && $importe_parcial <= 9.99)
$car = 1;
else if ($importe_parcial >= 10 && $importe_parcial <= 99.99)
$car = 2;
else if ($importe_parcial >= 100 && $importe_parcial <= 999.99)
$car = 3;

$parcial = substr($importe_parcial, 0, $car);
$importe_parcial = substr($importe_parcial, $car);

$num_letra = centena($parcial).centimos();

return $num_letra;
}

function cien_mil()
{
global $importe_parcial;

$parcial = 0; $car = 0;

while (substr($importe_parcial, 0, 1) == 0)
$importe_parcial = substr($importe_parcial, 1, strlen($importe_parcial) - 1);

if ($importe_parcial >= 1000 && $importe_parcial <= 9999.99)
$car = 1;
else if ($importe_parcial >= 10000 && $importe_parcial <= 99999.99)
$car = 2;
else if ($importe_parcial >= 100000 && $importe_parcial <= 999999.99)
$car = 3;

$parcial = substr($importe_parcial, 0, $car);
$importe_parcial = substr($importe_parcial, $car);

if ($parcial > 0)
{
if ($parcial == 1)
$num_letra = "mil ";
else
$num_letra = centena($parcial)." mil ";
}

return $num_letra;
}


function millon()
{
global $importe_parcial;

$parcial = 0; $car = 0;

while (substr($importe_parcial, 0, 1) == 0)
$importe_parcial = substr($importe_parcial, 1, strlen($importe_parcial) - 1);

if ($importe_parcial >= 1000000 && $importe_parcial <= 9999999.99)
$car = 1;
else if ($importe_parcial >= 10000000 && $importe_parcial <= 99999999.99)
$car = 2;
else if ($importe_parcial >= 100000000 && $importe_parcial <= 999999999.99)
$car = 3;

$parcial = substr($importe_parcial, 0, $car);
$importe_parcial = substr($importe_parcial, $car);

if ($parcial == 1)
$num_letras = "un millón ";
else
$num_letras = centena($parcial)." millones ";

return $num_letras;
}

function convertir_a_letras($numero)
{
global $importe_parcial;

$importe_parcial = $numero;

if ($numero < 1000000000)
{
if ($numero >= 1000000 && $numero <= 999999999.99)
$num_letras = millon().cien_mil().cien();
else if ($numero >= 1000 && $numero <= 999999.99)
$num_letras = cien_mil().cien();
else if ($numero >= 1 && $numero <= 999.99)
$num_letras = cien();
else if ($numero >= 0.01 && $numero <= 0.99)
{
if ($numero == 0.01)
$num_letras = "un centavo";
else
$num_letras = convertir_a_letras(($numero * 100)."/100")." centavos";
}
}
return $num_letras;
}
?>
En línea

Páginas: [1] Ir Arriba Respuesta Imprimir 

Ir a:  

WAP2 - Aviso Legal - Powered by SMF 1.1.21 | SMF © 2006-2008, Simple Machines