Ver Mensaje Individual
  #1 (permalink)  
Antiguo 20/10/2005, 09:39
Avatar de Baby78
Baby78
 
Fecha de Ingreso: julio-2005
Mensajes: 142
Antigüedad: 18 años, 8 meses
Puntos: 0
Convertir de numeros a letras con decimales

Hola a todos,navegando encontre el siguiente codigo (no recuerdo el lugar)

Cita:
<html>
<head>

<title>Convierte de números a letras</title>
<script language="JavaScript">
<!--

aUnits=["","One","Two","Three","Four","Five","Six","Seven" ,"Eight","Nine"]
aTeens=["","Eleven","Twelve","Thirteen","Fourteen","Fiftee n","Sixteen","Seventeen","Eighteen","Nineteen"]
aTens =["","Ten","Twenty","Thirty","Forty","Fifty","Sixty" ,"Seventy","Eighty","Ninety"]

function getunits(xValue){
/* This function accepts a one string parameter i.e. "1", "0", "9" etc... */
return (aUnits[xValue]);
}

function gethund(xValue){
/* This function accepts a one string parameter i.e. "1", "0", "9" etc... */
if (aUnits[xValue] != '')
return (aUnits[xValue]+' Hundred');
else
return (aUnits[xValue])
}

function teens(xValue){
return (aTeens[xValue])
}

function tens(xValue){
return (aTens[xValue])
}

function numtowords(xamount){
/*
stramt = amount entered converted to a string
xmax = 12 (999 million max) minus the converted string length.
xstramt = actual dollars amount without decimal places.
str1..4 = break down of actual string

*/

var stramt = ""+xamount
var xstramt=''
var xstr=""

// if a decimal place is not given, then add it.
if (stramt.indexOf(".") == -1)
stramt=stramt+'.00'

var xmax= 12 - stramt.length
// preceed the string with zero fillings to 12 characters (999 million maximum)
for (c=1; c <= xmax; c++){
xstramt += '0'
}
stramt=xstramt+stramt

xstramt=stramt.substring(0,9)

str1= stramt.substring(0,3)
str2= stramt.substring(3,6)
str3= stramt.substring(6,9)
str4= stramt.substring(10,12)

// if millions add label and reduce the remaining string length.
if (xstramt.length >=7){
xstr_=numwords(str1)
if (xstr_.length >= 3)
xstr+=xstr_+" Million "
xstramt=xstramt.substring(3,9)
}

// if thousands add label and reduce the remaining string length.
if (xstramt.length >= 4){
xstr_=numwords(str2)
if (xstr_.length >= 3)
xstr+=xstr_+" Thousand "
xstramt=xstramt.substring(6,9)
}

// hundreds or tens
if (xstramt.length <= 3){
xstr_=numwords(str3)
xstr+=xstr_+' '
}

xstr+='and '+str4+'/100' // optional if cents x/100.
//xstr+='and '+gettens(str4)+' cents' // optional if cents to be words.
return (xstr)
}


function numwords(xstr){
xhund =gethund(xstr.substring(0,1))
xtens =gettens(xstr.substring(1,3))
xwords=xhund
if (xtens.length > 3)
xwords+=" "+xtens
return (xwords);
}

function gettens(xValue){

/* This function accepts a two string parameter i.e. "90", "12", "32" etc...

xval_ = the first character.
_xval = the second character.

*/
xval_ =xValue.substring(0,1);
_xval =xValue.substring(1,2);

if (xval_ == '1' && _xval != '0')
xnum=teens(_xval)

if (xval_ == '1' && _xval == '0')
xnum="Ten"

if (xval_ != '1' && (_xval != '0' || _xval == '0')){
xnum=tens(xval_)
xnum+=" "+getunits(_xval);}

return (xnum)
}

function go(){
xvalue= prompt('Entra la cantidad','');

if (xvalue >= 1)
alert('El valor es: '+xvalue+'\n\n En letras: '+numtowords(xvalue))
else
alert('Cantidad mínima = $1.00: Ejecución abortada')
}

//-->
</script>


</head>

<body text="black" vLink="gray" aLink="red" link="blue" bgColor="white">


<script> numtoWords(125.36)</script>

<div align="center">
<h2><font size="3">Convertidor de números a letras</font><br>
</h2>
<form>
<p>

<input onclick="go()" type="button" value="Pulsa aquí para introducir la cantidad">
</p>
</form>
<p>&nbsp;</div>


</body>

</html>
La cuestion es que lo necesito, pero como soy nueva en javascript no se por donde empezar. A ver si me puedo explicar.

Tengo una cantidad en mi base de datos la cual la mando llamar por medio de asp realizo algunos calculos con ella y su valor final lo almaceno en una session, mi problema es que ese valor necesito convertirlo en letra, pero directamente y no preguntando el valor a convertir. El ejemplo que me encontre pregunta el valor y la verdad no se como hacerle para que no me pregunte y lo haga directamente (claro tomando el valor almacenado)

Si alguien me puede hechar la mano se los agradeceria de todo corazon, ya que voy a tener revision de residencias y no se que hacer.

Gracias..

Última edición por Baby78; 20/10/2005 a las 10:05