Dom Nov 30, 2008 11:33 am |
|
|
luisbal
Perlero Nuevo
|
Registrado: Vie Sep 12, 2008 9:36 am Mensajes: 16
|
|
|
Convertir números a letras
|
Estimados amigos: Me devano los sesos y no doy con el error. Este script convierte números a letras, me parece muy útil. El problema es que cuando el número decimal es igual o mayor que 0.50 devuelve el entero aumentado en uno. Por ejemplo: 23.40 devuelve veintitrés y 40 (bien) pero en 23.50 devuelve veinticuatro y 50 (error) Les envió el script a ver si solucionan el error, les agradezco cualquier ayuda. Using javascript Syntax Highlighting <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script language="javascript">
// Función modulo, regresa el residuo de una división
function mod(dividendo , divisor)
{
resDiv = dividendo / divisor ;
parteEnt = Math.floor(resDiv); // Obtiene la parte Entera de resDiv
parteFrac = resDiv - parteEnt ; // Obtiene la parte Fraccionaria de la división
modulo = Math.round(parteFrac * divisor); // Regresa la parte fraccionaria * la división (modulo)
return modulo;
} // Fin de función mod
// Función ObtenerParteEntDiv, regresa la parte entera de una división
function ObtenerParteEntDiv(dividendo , divisor)
{
resDiv = dividendo / divisor ;
parteEntDiv = Math.floor(resDiv);
return parteEntDiv;
} // Fin de función ObtenerParteEntDiv
// function fraction_part, regresa la parte Fraccionaria de una cantidad
function fraction_part(dividendo , divisor)
{
resDiv = dividendo / divisor ;
f_part = Math.floor(resDiv);
return f_part;
} // Fin de función fraction_part
// function string_literal conversion is the core of this program
// converts numbers to spanish strings, handling the general special
// cases in spanish language.
function string_literal_conversion(number)
{
// first, divide your number in hundreds, tens and units, cascadig
// trough subsequent divisions, using the modulus of each division
// for the next.
centenas = ObtenerParteEntDiv(number, 100);
number = mod(number, 100);
decenas = ObtenerParteEntDiv(number, 10);
number = mod(number, 10);
unidades = ObtenerParteEntDiv(number, 1);
number = mod(number, 1);
string_hundreds="";
string_tens="";
string_units="";
// cascade trough hundreds. This will convert the hundreds part to
// their corresponding string in spanish.
if(centenas == 1){
string_hundreds = "ciento ";
}
if(centenas == 2){
string_hundreds = "doscientos ";
}
if(centenas == 3){
string_hundreds = "trescientos ";
}
if(centenas == 4){
string_hundreds = "cuatrocientos ";
}
if(centenas == 5){
string_hundreds = "quinientos ";
}
if(centenas == 6){
string_hundreds = "seiscientos ";
}
if(centenas == 7){
string_hundreds = "setecientos ";
}
if(centenas == 8){
string_hundreds = "ochocientos ";
}
if(centenas == 9){
string_hundreds = "novecientos ";
}
// end switch hundreds
// casgade trough tens. This will convert the tens part to corresponding
// strings in spanish. Note, however that the strings between 11 and 19
// are all special cases. Also 21-29 is a special case in spanish.
if(decenas == 1){
//Special case, depends on units for each conversion
if(unidades == 1){
string_tens = "once";
}
if(unidades == 2){
string_tens = "doce";
}
if(unidades == 3){
string_tens = "trece";
}
if(unidades == 4){
string_tens = "catorce";
}
if(unidades == 5){
string_tens = "quince";
}
if(unidades == 6){
string_tens = "dieciseis";
}
if(unidades == 7){
string_tens = "diecisiete";
}
if(unidades == 8){
string_tens = "dieciocho";
}
if(unidades == 9){
string_tens = "diecinueve";
}
}
//alert("STRING_TENS ="+string_tens);
if(decenas == 2){
string_tens = "veinti";
}
if(decenas == 3){
string_tens = "treinta";
}
if(decenas == 4){
string_tens = "cuarenta";
}
if(decenas == 5){
string_tens = "cincuenta";
}
if(decenas == 6){
string_tens = "sesenta";
}
if(decenas == 7){
string_tens = "setenta";
}
if(decenas == 8){
string_tens = "ochenta";
}
if(decenas == 9){
string_tens = "noventa";
}
// Fin de swicth decenas
// cascades trough units, This will convert the units part to corresponding
// strings in spanish. Note however that a check is being made to see wether
// the special cases 11-19 were used. In that case, the whole conversion of
// individual units is ignored since it was already made in the tens cascade.
if (decenas == 1)
{
string_units=""; // empties the units check, since it has alredy been handled on the tens switch
}
else
{
if(unidades == 1){
string_units = "un";
}
if(unidades == 2){
string_units = "dos";
}
if(unidades == 3){
string_units = "tres";
}
if(unidades == 4){
string_units = "cuatro";
}
if(unidades == 5){
string_units = "cinco";
}
if(unidades == 6){
string_units = "seis";
}
if(unidades == 7){
string_units = "siete";
}
if(unidades == 8){
string_units = "ocho";
}
if(unidades == 9){
string_units = "nueve";
}
// end switch units
} // end if-then-else
//final special cases. This conditions will handle the special cases which
//are not as general as the ones in the cascades. Basically four:
// when you've got 100, you dont' say 'ciento' you say 'cien'
// 'ciento' is used only for [101 >= number > 199]
if (centenas == 1 && decenas == 0 && unidades == 0)
{
string_hundreds = "cien " ;
}
// when you've got 10, you don't say any of the 11-19 special
// cases.. just say 'diez'
if (decenas == 1 && unidades ==0)
{
string_tens = "diez " ;
}
// when you've got 20, you don't say 'veinti', which is used
// only for [21 >= number > 29]
if (decenas == 2 && unidades ==0)
{
string_tens = "veinte " ;
}
// for numbers >= 30, you don't use a single word such as veintiuno
// (twenty one), you must add 'y' (and), and use two words. v.gr 31
// 'treinta y uno' (thirty and one)
if (decenas >=3 && unidades >=1)
{
string_tens = string_tens+" y ";
}
// this line gathers all the hundreds, tens and units into the final string
// and returns it as the function value.
final_string = string_hundreds+string_tens+string_units;
return final_string ;
} //end of function string_literal_conversion()================================
// handle some external special cases. Specially the millions, thousands
// and hundreds descriptors. Since the same rules apply to all number triads
// descriptions are handled outside the string conversion function, so it can
// be re used for each triad.
function covertirNumLetras(number)
{
//number = number_format (number, 2);
number1=number;
//settype (number, "integer");
cent = number1.split(".");
centavos = cent[1];
if (centavos == 0 || centavos == undefined){
centavos = "00";}
if (number == 0 || number == "")
{ // if amount = 0, then forget all about conversions,
centenas_final_string=" cero "; // amount is zero (cero). handle it externally, to
// function breakdown
}
else
{
millions = ObtenerParteEntDiv(number, 1000000); // first, send the millions to the string
number = mod(number, 1000000); // conversion function
if (millions != 0)
{
// This condition handles the plural case
if (millions == 1)
{ // if only 1, use 'millon' (million). if
descriptor= " millon "; // > than 1, use 'millones' (millions) as
}
else
{ // a descriptor for this triad.
descriptor = " millones ";
}
}
else
{
descriptor = " "; // if 0 million then use no descriptor.
}
millions_final_string = string_literal_conversion(millions)+descriptor;
thousands = ObtenerParteEntDiv(number, 1000); // now, send the thousands to the string
number = mod(number, 1000); // conversion function.
//print "Th:".thousands;
if (thousands != 1)
{ // This condition eliminates the descriptor
thousands_final_string =string_literal_conversion(thousands) + " mil ";
// descriptor = " mil "; // if there are no thousands on the amount
}
if (thousands == 1)
{
thousands_final_string = " mil ";
}
if (thousands < 1)
{
thousands_final_string = " ";
}
// this will handle numbers between 1 and 999 which
// need no descriptor whatsoever.
centenas = number;
centenas_final_string = string_literal_conversion(centenas) ;
} //end if (number ==0)
/*if (ereg("un",centenas_final_string))
{
centenas_final_string = ereg_replace("","o",centenas_final_string);
}*/
//finally, print the output.
/* Concatena los millones, miles y cientos*/
cad = millions_final_string+thousands_final_string+centenas_final_string;
/* Convierte la cadena a Mayúsculas*/
cad = cad.toUpperCase();
if (centavos.length>2)
{
if(centavos.substring(2,3)>= 5){
centavos = centavos.substring(0,1)+(parseInt(centavos.substring(1,2))+1).toString();
} else{
centavos = centavos.substring(0,2);
}
}
/* Concatena a los centavos la cadena "/100" */
if (centavos.length==1)
{
centavos = centavos+"0";
}
centavos = centavos+ "/100";
/* Asigna el tipo de moneda, para 1 = PESO, para distinto de 1 = PESOS*/
if (number == 1)
{
moneda = " PESO ";
}
else
{
moneda = " PESOS ";
}
/* Regresa el número en cadena entre paréntesis y con tipo de moneda y la fase M.N.*/
alert( "FINAL="+cad+moneda+centavos+" 00/100 M.N. )");
}
</script>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<script> covertirNumLetras("3.20");</script>
</body>
</html>
|
Vie Ago 20, 2010 1:47 pm |
|
|
Mind800
Perlero Nuevo
|
Registrado: Vie Ago 20, 2010 12:32 pm Mensajes: 2
|
|
|
Re: Convertir números a letras RESUELTO
|
Hola, buscando en la red una manera de convertir cantidades numéricas a letras encontré tu script, el cual está muy bueno, solo con el error que mencionas. Así que me di a la tarea de revisar el código, y encontré la respuesta, en la parte de abajo separas los centavos del número para redondearlos y trabajarlos a parte: Using javascript Syntax Highlighting number1 = number;
cent = number1.split(".");
centavos = cent[1];
El problema radica en que si ya separaste los centavos, no sobreescribes la variable "number" para dejar solo el número entero, la dejas tal cual con centavos. La función original por lo visto no estaba pensada para usar centavos puesto que hay una función que hace un "math.round" y como le dejas los centavos redondea el número. Para corregir el error basta con agregar esta línea después de "centavos = cent[1];" Using javascript Syntax Highlighting //Mind Mod
number = cent[0];
Con esto ya no habrá el problema del número redondeado. Ya sé que el tema es muy viejo y a lo mejor ya resolviste el problema, pero a lo mejor alguien más como yo necesita esta función y se topa con el post. De esta manera ya podrá encontrarla lista para usar. Aquí pongo de nuevo el código completo ya corregido y con unas modificaciones para el caso de 1000 pesos. Saludos. Using javascript Syntax Highlighting <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script language="javascript">
// Función modulo, regresa el residuo de una división
function mod(dividendo , divisor)
{
resDiv = dividendo / divisor ;
parteEnt = Math.floor(resDiv); // Obtiene la parte Entera de resDiv
parteFrac = resDiv - parteEnt ; // Obtiene la parte Fraccionaria de la división
//modulo = parteFrac * divisor; // Regresa la parte fraccionaria * la división (modulo)
modulo = Math.round(parteFrac * divisor)
return modulo;
} // Fin de función mod
// Función ObtenerParteEntDiv, regresa la parte entera de una división
function ObtenerParteEntDiv(dividendo , divisor)
{
resDiv = dividendo / divisor ;
parteEntDiv = Math.floor(resDiv);
return parteEntDiv;
} // Fin de función ObtenerParteEntDiv
// function fraction_part, regresa la parte Fraccionaria de una cantidad
function fraction_part(dividendo , divisor)
{
resDiv = dividendo / divisor ;
f_part = Math.floor(resDiv);
return f_part;
} // Fin de función fraction_part
// function string_literal conversion is the core of this program
// converts numbers to spanish strings, handling the general special
// cases in spanish language.
function string_literal_conversion(number)
{
// first, divide your number in hundreds, tens and units, cascadig
// trough subsequent divisions, using the modulus of each division
// for the next.
centenas = ObtenerParteEntDiv(number, 100);
number = mod(number, 100);
decenas = ObtenerParteEntDiv(number, 10);
number = mod(number, 10);
unidades = ObtenerParteEntDiv(number, 1);
number = mod(number, 1);
string_hundreds="";
string_tens="";
string_units="";
// cascade trough hundreds. This will convert the hundreds part to
// their corresponding string in spanish.
if(centenas == 1){
string_hundreds = "ciento ";
}
if(centenas == 2){
string_hundreds = "doscientos ";
}
if(centenas == 3){
string_hundreds = "trescientos ";
}
if(centenas == 4){
string_hundreds = "cuatrocientos ";
}
if(centenas == 5){
string_hundreds = "quinientos ";
}
if(centenas == 6){
string_hundreds = "seiscientos ";
}
if(centenas == 7){
string_hundreds = "setecientos ";
}
if(centenas == 8){
string_hundreds = "ochocientos ";
}
if(centenas == 9){
string_hundreds = "novecientos ";
}
// end switch hundreds
// casgade trough tens. This will convert the tens part to corresponding
// strings in spanish. Note, however that the strings between 11 and 19
// are all special cases. Also 21-29 is a special case in spanish.
if(decenas == 1){
//Special case, depends on units for each conversion
if(unidades == 1){
string_tens = "once";
}
if(unidades == 2){
string_tens = "doce";
}
if(unidades == 3){
string_tens = "trece";
}
if(unidades == 4){
string_tens = "catorce";
}
if(unidades == 5){
string_tens = "quince";
}
if(unidades == 6){
string_tens = "dieciseis";
}
if(unidades == 7){
string_tens = "diecisiete";
}
if(unidades == 8){
string_tens = "dieciocho";
}
if(unidades == 9){
string_tens = "diecinueve";
}
}
//alert("STRING_TENS ="+string_tens);
if(decenas == 2){
string_tens = "veinti";
}
if(decenas == 3){
string_tens = "treinta";
}
if(decenas == 4){
string_tens = "cuarenta";
}
if(decenas == 5){
string_tens = "cincuenta";
}
if(decenas == 6){
string_tens = "sesenta";
}
if(decenas == 7){
string_tens = "setenta";
}
if(decenas == 8){
string_tens = "ochenta";
}
if(decenas == 9){
string_tens = "noventa";
}
// Fin de swicth decenas
// cascades trough units, This will convert the units part to corresponding
// strings in spanish. Note however that a check is being made to see wether
// the special cases 11-19 were used. In that case, the whole conversion of
// individual units is ignored since it was already made in the tens cascade.
if (decenas == 1)
{
string_units=""; // empties the units check, since it has alredy been handled on the tens switch
}
else
{
if(unidades == 1){
string_units = "un";
}
if(unidades == 2){
string_units = "dos";
}
if(unidades == 3){
string_units = "tres";
}
if(unidades == 4){
string_units = "cuatro";
}
if(unidades == 5){
string_units = "cinco";
}
if(unidades == 6){
string_units = "seis";
}
if(unidades == 7){
string_units = "siete";
}
if(unidades == 8){
string_units = "ocho";
}
if(unidades == 9){
string_units = "nueve";
}
// end switch units
} // end if-then-else
//final special cases. This conditions will handle the special cases which
//are not as general as the ones in the cascades. Basically four:
// when you've got 100, you dont' say 'ciento' you say 'cien'
// 'ciento' is used only for [101 >= number > 199]
if (centenas == 1 && decenas == 0 && unidades == 0)
{
string_hundreds = "cien " ;
}
// when you've got 10, you don't say any of the 11-19 special
// cases.. just say 'diez'
if (decenas == 1 && unidades ==0)
{
string_tens = "diez " ;
}
// when you've got 20, you don't say 'veinti', which is used
// only for [21 >= number > 29]
if (decenas == 2 && unidades ==0)
{
string_tens = "veinte " ;
}
// for numbers >= 30, you don't use a single word such as veintiuno
// (twenty one), you must add 'y' (and), and use two words. v.gr 31
// 'treinta y uno' (thirty and one)
if (decenas >=3 && unidades >=1)
{
string_tens = string_tens+" y ";
}
// this line gathers all the hundreds, tens and units into the final string
// and returns it as the function value.
final_string = string_hundreds+string_tens+string_units;
return final_string ;
} //end of function string_literal_conversion()================================
// handle some external special cases. Specially the millions, thousands
// and hundreds descriptors. Since the same rules apply to all number triads
// descriptions are handled outside the string conversion function, so it can
// be re used for each triad.
function covertirNumLetras(number)
{
//number = number_format (number, 2);
number1=number;
//settype (number, "integer");
cent = number1.split(".");
centavos = cent[1];
//Mind Mod
number=cent[0];
if (centavos == 0 || centavos == undefined)
{
centavos = "00";
}
if (number == 0 || number == "")
{ // if amount = 0, then forget all about conversions,
centenas_final_string=" cero "; // amount is zero (cero). handle it externally, to
// function breakdown
}
else
{
millions = ObtenerParteEntDiv(number, 1000000); // first, send the millions to the string
number = mod(number, 1000000); // conversion function
if (millions != 0)
{
// This condition handles the plural case
if (millions == 1)
{ // if only 1, use 'millon' (million). if
descriptor= " millon "; // > than 1, use 'millones' (millions) as
}
else
{ // a descriptor for this triad.
descriptor = " millones ";
}
}
else
{
descriptor = " "; // if 0 million then use no descriptor.
}
millions_final_string = string_literal_conversion(millions)+descriptor;
thousands = ObtenerParteEntDiv(number, 1000); // now, send the thousands to the string
number = mod(number, 1000); // conversion function.
//print "Th:".thousands;
if (thousands != 1)
{ // This condition eliminates the descriptor
thousands_final_string =string_literal_conversion(thousands) + " mil ";
// descriptor = " mil "; // if there are no thousands on the amount
}
if (thousands == 1)
{
thousands_final_string = " mil ";
}
if (thousands < 1)
{
thousands_final_string = " ";
}
// this will handle numbers between 1 and 999 which
// need no descriptor whatsoever.
centenas = number;
centenas_final_string = string_literal_conversion(centenas) ;
} //end if (number ==0)
/*if (ereg("un",centenas_final_string))
{
centenas_final_string = ereg_replace("","o",centenas_final_string);
}*/
//finally, print the output.
/* Concatena los millones, miles y cientos*/
cad = millions_final_string+thousands_final_string+centenas_final_string;
/* Convierte la cadena a Mayúsculas*/
cad = cad.toUpperCase();
if (centavos.length>2)
{
if(centavos.substring(2,3)>= 5){
centavos = centavos.substring(0,1)+(parseInt(centavos.substring(1,2))+1).toString();
} else{
centavos = centavos.substring(0,1);
}
}
/* Concatena a los centavos la cadena "/100" */
if (centavos.length==1)
{
centavos = centavos+"0";
}
centavos = centavos+ "/100";
/* Asigna el tipo de moneda, para 1 = PESO, para distinto de 1 = PESOS*/
if (number == 1)
{
moneda = " PESO ";
}
else
{
moneda = " PESOS ";
}
/* Regresa el número en cadena entre paréntesis y con tipo de moneda y la fase M.N.*/
//Mind Mod, si se deja MIL pesos y se utiliza esta función para imprimir documentos
//de caracter legal, dejar solo MIL es incorrecto, para evitar fraudes se debe de poner UM MIL pesos
if(cad == ' MIL ')
{
cad=' UN MIL ';
}
alert( "FINAL="+cad+moneda+centavos+" M.N.");
return cad+moneda+centavos+" M.N.";
}
</script>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
//Mind Mod - numero de 12 millones con varios decimales para que comprueben su efectividad
<script> covertirNumLetras("12456789.459786");</script>
</body>
</html>
|
Vie Ago 20, 2010 2:45 pm |
|
|
luisbal
Perlero Nuevo
|
Registrado: Vie Sep 12, 2008 9:36 am Mensajes: 16
|
|
|
Re: Convertir números a letras
|
|
No que va te agradezco, en nombre pienso yo de todos los demás internautas.
En efecto este problema lo tuve hace tiempo y nunca llegué a resolverlo: opté por utilizar un módulo de Perl que me facilitó la tarea. De todos modos estoy copiando tu solución para guardarla en mi PC: nunca se sabe cuándo volverás a utilizarlo.
Muchas gracias, de verdad.
|
Vie Ago 20, 2010 3:01 pm |
|
|
Mind800
Perlero Nuevo
|
Registrado: Vie Ago 20, 2010 12:32 pm Mensajes: 2
|
|
|
Re: Convertir números a letras
|
|
De nada, espero que esto sea de ayuda para alguien más, como lo fue para mi, el script que publicaste me ahorró mucho tiempo, lo mínimo que podía hacer era contribuir para que ayude a alguien más.
|
Dom Sep 26, 2010 1:35 pm |
|
|
Dodomac
Perlero Nuevo
|
Registrado: Dom Sep 26, 2010 12:44 pm Mensajes: 1
|
|
|
Re: Convertir números a letras
|
Es ahora que necesité rápidamente hacer esta conversión para una plantilla de factura. Les agradezco que lo hayan publicado, ya que me sacó de apuros. Al implementarlo encontré un "medio error"... se trata de que la función function convertirNumLetras(number) {} interpreta el [i]number[/i] como [i]string[/i] y falta convertir el valor en [i]string[/i]. Aquí está mi modificación: Using javascript Syntax Highlighting number1=number.toString(); En lugar de: Using javascript Syntax Highlighting Para capturar el valor de una variable de otro lenguaje (PHP, Python, etc) le agregué esta función y lo relacioné con el evento "onLoad" en el BODY: Using javascript Syntax Highlighting function doSpanish(importe) {
document.getElementById('spn').innerHTML='( '+convertirNumLetras(importe)+ ' )';
return true;
}
Contenido HTML: Using javascript Syntax Highlighting <body onLoad="doSpanish($variabilaValue)">
<div id="spn" style="float:left;margin:40px auto;">XXX</div>
Para estar más claro, aquí está todo con las modificaciones. Espero que les sirvan a otros también Using javascript Syntax Highlighting <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Converción importe a letras</title>
<script type="application/javascript">
// Función modulo, regresa el residuo de una división
function mod(dividendo , divisor)
{
resDiv = dividendo / divisor ;
parteEnt = Math.floor(resDiv); // Obtiene la parte Entera de resDiv
parteFrac = resDiv - parteEnt ; // Obtiene la parte Fraccionaria de la división
//modulo = parteFrac * divisor; // Regresa la parte fraccionaria * la división (modulo)
modulo = Math.round(parteFrac * divisor)
return modulo;
}
// Fin de función mod
// Función ObtenerParteEntDiv, regresa la parte entera de una división
function ObtenerParteEntDiv(dividendo , divisor)
{
resDiv = dividendo / divisor ;
parteEntDiv = Math.floor(resDiv);
return parteEntDiv;
}
// Fin de función ObtenerParteEntDiv
// function fraction_part, regresa la parte Fraccionaria de una cantidad
function fraction_part(dividendo , divisor)
{
resDiv = dividendo / divisor ;
f_part = Math.floor(resDiv);
return f_part;
}
// Fin de función fraction_part
// function string_literal conversion is the core of this program
// converts numbers to spanish strings, handling the general special
// cases in spanish language.
function string_literal_conversion(number)
{
// first, divide your number in hundreds, tens and units, cascadig
// trough subsequent divisions, using the modulus of each division
// for the next.
centenas = ObtenerParteEntDiv(number, 100);
number = mod(number, 100);
decenas = ObtenerParteEntDiv(number, 10);
number = mod(number, 10);
unidades = ObtenerParteEntDiv(number, 1);
number = mod(number, 1);
string_hundreds="";
string_tens="";
string_units="";
// cascade trough hundreds. This will convert the hundreds part to
// their corresponding string in spanish.
if(centenas == 1){
string_hundreds = "ciento ";
}
if(centenas == 2){
string_hundreds = "doscientos ";
}
if(centenas == 3){
string_hundreds = "trescientos ";
}
if(centenas == 4){
string_hundreds = "cuatrocientos ";
}
if(centenas == 5){
string_hundreds = "quinientos ";
}
if(centenas == 6){
string_hundreds = "seiscientos ";
}
if(centenas == 7){
string_hundreds = "setecientos ";
}
if(centenas == <img src="http://perlenespanol.com/foro/images/smilies/icon_cool.gif" alt="8)" title="Cool" />{
string_hundreds = "ochocientos ";
}
if(centenas == 9){
string_hundreds = "novecientos ";
}
// end switch hundreds
// casgade trough tens. This will convert the tens part to corresponding
// strings in spanish. Note, however that the strings between 11 and 19
// are all special cases. Also 21-29 is a special case in spanish.
if(decenas == 1){
//Special case, depends on units for each conversion
if(unidades == 1){
string_tens = "once";
}
if(unidades == 2){
string_tens = "doce";
}
if(unidades == 3){
string_tens = "trece";
}
if(unidades == 4){
string_tens = "catorce";
}
if(unidades == 5){
string_tens = "quince";
}
if(unidades == 6){
string_tens = "dieciseis";
}
if(unidades == 7){
string_tens = "diecisiete";
}
if(unidades == <img src="http://perlenespanol.com/foro/images/smilies/icon_cool.gif" alt="8)" title="Cool" />{
string_tens = "dieciocho";
}
if(unidades == 9){
string_tens = "diecinueve";
}
}
//alert("STRING_TENS ="+string_tens);
if(decenas == 2){
string_tens = "veinti";
}
if(decenas == 3){
string_tens = "treinta";
}
if(decenas == 4){
string_tens = "cuarenta";
}
if(decenas == 5){
string_tens = "cincuenta";
}
if(decenas == 6){
string_tens = "sesenta";
}
if(decenas == 7){
string_tens = "setenta";
}
if(decenas == <img src="http://perlenespanol.com/foro/images/smilies/icon_cool.gif" alt="8)" title="Cool" />{
string_tens = "ochenta";
}
if(decenas == 9){
string_tens = "noventa";
}
// Fin of swicth decenas
// cascades trough units, This will convert the units part to corresponding
// strings in spanish. Note however that a check is being made to see wether
// the special cases 11-19 were used. In that case, the whole conversion of
// individual units is ignored since it was already made in the tens cascade.
if (decenas == 1)
{
string_units="";
// empties the units check, since it has alredy been handled on the tens switch
}
else
{
if(unidades == 1){
string_units = "un";
}
if(unidades == 2){
string_units = "dos";
}
if(unidades == 3){
string_units = "tres";
}
if(unidades == 4){
string_units = "cuatro";
}
if(unidades == 5){
string_units = "cinco";
}
if(unidades == 6){
string_units = "seis";
}
if(unidades == 7){
string_units = "siete";
}
if(unidades == <img src="http://perlenespanol.com/foro/images/smilies/icon_cool.gif" alt="8)" title="Cool" />{
string_units = "ocho";
}
if(unidades == 9){
string_units = "nueve";
}
// end switch units
} // end if-then-else
//final special cases. This conditions will handle the special cases which
//are not as general as the ones in the cascades. Basically four:
// when you've got 100, you dont' say 'ciento' you say 'cien'
// 'ciento' is used only for [101 >= number > 199]
if (centenas == 1 && decenas == 0 && unidades == 0)
{
string_hundreds = "cien " ;
}
// when you've got 10, you don't say any of the 11-19 special
// cases.. just say 'diez'
if (decenas == 1 && unidades ==0)
{
string_tens = "diez " ;
}
// when you've got 20, you don't say 'veinti', which is used
// only for [21 >= number > 29]
if (decenas == 2 && unidades ==0)
{
string_tens = "veinte " ;
}
// for numbers >= 30, you don't use a single word such as veintiuno
// (twenty one), you must add 'y' (and), and use two words. v.gr 31
// 'treinta y uno' (thirty and one)
if (decenas >=3 && unidades >=1)
{
string_tens = string_tens+" y ";
}
// this line gathers all the hundreds, tens and units into the final string
// and returns it as the function value.
final_string = string_hundreds+string_tens+string_units;
return final_string ;
}
//end of function string_literal_conversion
// handle some external special cases. Specially the millions, thousands
// and hundreds descriptors. Since the same rules apply to all number triads
// descriptions are handled outside the string conversion function, so it can
// be re used for each triad.
function convertirNumLetras(number)
{
//number = number_format (number, 2);
number1=number.toString();
//settype (number, "integer");
cent = number1.split(".");
centavos = cent[1];
//Mind Mod
number=cent[0];
if (centavos == 0 || centavos == undefined)
{
centavos = "00";
}
if (number == 0 || number == "")
{ // if amount = 0, then forget all about conversions,
centenas_final_string=" cero "; // amount is zero (cero). handle it externally, to
// function breakdown
}
else
{
millions = ObtenerParteEntDiv(number, 1000000); // first, send the millions to the string
number = mod(number, 1000000); // conversion function
if (millions != 0)
{
// This condition handles the plural case
if (millions == 1)
{ // if only 1, use 'millon' (million). if
descriptor= " millon "; // > than 1, use 'millones' (millions) as
}
else
{ // a descriptor for this triad.
descriptor = " millones ";
}
}
else
{
descriptor = " "; // if 0 million then use no descriptor.
}
millions_final_string = string_literal_conversion(millions)+descriptor;
thousands = ObtenerParteEntDiv(number, 1000); // now, send the thousands to the string
number = mod(number, 1000); // conversion function.
//print "Th:".thousands;
if (thousands != 1)
{ // This condition eliminates the descriptor
thousands_final_string =string_literal_conversion(thousands) + " mil ";
// descriptor = " mil "; // if there are no thousands on the amount
}
if (thousands == 1)
{
thousands_final_string = " mil ";
}
if (thousands < 1)
{
thousands_final_string = " ";
}
// this will handle numbers between 1 and 999 which
// need no descriptor whatsoever.
centenas = number;
centenas_final_string = string_literal_conversion(centenas) ;
} //end if (number ==0)
/*if (ereg("un",centenas_final_string))
{
centenas_final_string = ereg_replace("","o",centenas_final_string);
}*/
//finally, print the output.
/* Concatena los millones, miles y cientos*/
cad = millions_final_string+thousands_final_string+centenas_final_string;
/* Convierte la cadena a Mayúsculas*/
cad = cad.toUpperCase();
if (centavos.length>2)
{
if(centavos.substring(2,3)>= 5){
centavos = centavos.substring(0,1)+(parseInt(centavos.substring(1,2))+1).toString();
} else{
centavos = centavos.substring(0,1);
}
}
/* Concatena a los centavos la cadena "/100" */
if (centavos.length==1)
{
centavos = centavos+"0";
}
centavos = centavos+ "/100";
/* Asigna el tipo de moneda, para 1 = PESO, para distinto de 1 = PESOS*/
if (number == 1)
{
moneda = " PESO ";
}
else
{
moneda = " PESOS ";
}
/* Regresa el número en cadena entre paréntesis y con tipo de moneda y la fase M.N.*/
//Mind Mod, si se deja MIL pesos y se utiliza esta función para imprimir documentos
//de caracter legal, dejar solo MIL es incorrecto, para evitar fraudes se debe de poner UM MIL pesos
if(cad == ' MIL ')
{
cad=' UN MIL ';
}
// alert( "FINAL="+cad+moneda+centavos+" M.N.");
return cad+moneda+centavos+" M.N.";
}
function doSpanish(importe) {
document.getElementById('spn').innerHTML='( '+convertirNumLetras(importe)+ ' )';
return true;
}
</script>
</head>
<body onLoad="doSpanish($variabilaValue)">
<div id="spn" style="float:left;margin:40px auto;">XXX</div>
</body>
</html>
|
|
Página 1 de 1
|
[ 5 mensajes ] |
|
| Reglas del Foro |
No puedes abrir nuevos temas en este Foro No puedes responder a temas en este Foro No puedes editar tus mensajes en este Foro No puedes borrar tus mensajes en este Foro No puedes enviar adjuntos en este Foro
|
|
Socializa |
 |
|