Re: [PHP-ES] funcion para pasar numeros a letras

From: E. Ricardo Santos ( eddyrshot .en. hotmail.com)
Date: Sat Mar 06 2004 - 18:25:35 CET


Diego en Chile se manejan con billetes a partir de mil pesos por lo que una
factura bien puede ser de 5,000,000 millones de pesos. Esta funcionan se
utiliza actualmente en un sistema de facturacion para convertir el valor
total en letras e imprimirlo en el docuemnto. Por tanto no deberias tener
ningun problema para convertir hasta cifras de millones.

----- Original Message -----
From: "Diego Juritz" < djuritz .en. data54.com>
To: < php-es .en. lists.php.net>
Sent: Saturday, March 06, 2004 1:48 PM
Subject: Re: [PHP-ES] funcion para pasar numeros a letras

> Ricardo:
> gracias x la funcion.
> Pero ¿hasta que numeros sirve eso ?
> yo necesitaría alguna que convierta hasta 100.000 x lo menos.
>
> pego la funcion para que la prueben:
> el problema que tiene es que no anda con números redondos, es ridículo
pero
> es así!
> Por ej si ponen convertir_a_letras(1201), devuelve bien (mil doscientos
uno)
> pero si le tiran convertir_a_letras(12000), se queda ahi hasta que entra
en
> timeout.
>
> ¿alguna idea?
> x lo visto se cuelga con numeros redondos (que terminen en cero), mayores
a
> 10000
>
> Gracias...
>
> ==============================
> <?php
> // FUNCIONES DE CONVERSION DE NUMEROS A LETRAS.
> // Se llama a la función principal: convertir_a_letras($numero)
>
> 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 céntimos";
> break;
> }
> case 8:
> {
> $num_letra = "ocho céntimos";
> break;
> }
> case 7:
> {
> $num_letra = "siete céntimos";
> break;
> }
> case 6:
> {
> $num_letra = "seis céntimos";
> break;
> }
> case 5:
> {
> $num_letra = "cinco céntimos";
> break;
> }
> case 4:
> {
> $num_letra = "cuatro céntimos";
> break;
> }
> case 3:
> {
> $num_letra = "tres céntimos";
> break;
> }
> case 2:
> {
> $num_letra = "dos céntimos";
> break;
> }
> case 1:
> {
> $num_letra = "un céntimo";
> break;
> }
> }
> return $num_letra;
> }
>
>
> function decena_centimos($numero)
> {
> if ($numero >= 10)
> {
> if ($numero >= 90 && $numero <= 99)
> {
> if ($numero == 90)
> return "noventa céntimos";
> else if ($numero == 91)
> return "noventa y un céntimos";
> else
> return "noventa y ".unidad_centimos($numero - 90);
> }
> if ($numero >= 80 && $numero <= 89)
> {
> if ($numero == 80)
> return "ochenta céntimos";
> else if ($numero == 81)
> return "ochenta y un céntimos";
> else
> return "ochenta y ".unidad_centimos($numero - 80);
> }
> if ($numero >= 70 && $numero <= 79)
> {
> if ($numero == 70)
> return "setenta céntimos";
> else if ($numero == 71)
> return "setenta y un céntimos";
> else
> return "setenta y ".unidad_centimos($numero - 70);
> }
> if ($numero >= 60 && $numero <= 69)
> {
> if ($numero == 60)
> return "sesenta céntimos";
> else if ($numero == 61)
> return "sesenta y un céntimos";
> else
> return "sesenta y ".unidad_centimos($numero - 60);
> }
> if ($numero >= 50 && $numero <= 59)
> {
> if ($numero == 50)
> return "cincuenta céntimos";
> else if ($numero == 51)
> return "cincuenta y un céntimos";
> else
> return "cincuenta y ".unidad_centimos($numero - 50);
> }
> if ($numero >= 40 && $numero <= 49)
> {
> if ($numero == 40)
> return "cuarenta céntimos";
> else if ($numero == 41)
> return "cuarenta y un céntimos";
> else
> return "cuarenta y ".unidad_centimos($numero - 40);
> }
> if ($numero >= 30 && $numero <= 39)
> {
> if ($numero == 30)
> return "treinta céntimos";
> else if ($numero == 91)
> return "treinta y un céntimos";
> else
> return "treinta y ".unidad_centimos($numero - 30);
> }
> if ($numero >= 20 && $numero <= 29)
> {
> if ($numero == 20)
> return "veinte céntimos";
> else if ($numero == 21)
> return "veintiun céntimos";
> else
> return "veinti".unidad_centimos($numero - 20);
> }
> if ($numero >= 10 && $numero <= 19)
> {
> if ($numero == 10)
> return "diez céntimos";
> else if ($numero == 11)
> return "once céntimos";
> else if ($numero == 11)
> return "doce céntimos";
> else if ($numero == 11)
> return "trece céntimos";
> else if ($numero == 11)
> return "catorce céntimos";
> else if ($numero == 11)
> return "quince céntimos";
> else if ($numero == 11)
> return "dieciseis céntimos";
> else if ($numero == 11)
> return "diecisiete céntimos";
> else if ($numero == 11)
> return "dieciocho céntimos";
> else if ($numero == 11)
> return "diecinueve céntimos";
> }
> }
> 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 céntimo";
> else
> $num_letras = convertir_a_letras(($numero * 100)."/100")." céntimos";
> }
> }
> return $num_letras;
> }
> ?>
> ================================================
>
> --
> PHP Spanish Localization Talk Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>

-- 
PHP Spanish Localization Talk Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


This archive was generated by hypermail 2.1.7 : Fri May 14 2004 - 16:04:34 CEST