Anda di halaman 1dari 7

<script type="text/javascript">

function calcular_edad(dia_nacim,mes_nacim,anio_nacim)
{
fecha_hoy = new Date();
ahora_anio = fecha_hoy.getYear();
ahora_mes = fecha_hoy.getMonth();
ahora_dia = fecha_hoy.getDate();
edad = (ahora_anio + 1900) - anio_nacim;
if ( ahora_mes < (mes_nacim - 1))
{
edad--;
}
if (((mes_nacim - 1) == ahora_mes) && (ahora_dia < dia_nacim))
{
edad--;
}
if (edad > 1900)
{
edad -= 1900;
}
return edad;
}
</script>
---//calcular la edad de una persona
//recibe la fecha como un string en formato espaol
//devuelve un entero con la edad. Devuelve false en caso de que la fecha sea inc
orrecta o mayor que el dia actual
function calcular_edad(fecha){
//calculo la fecha de hoy
hoy=new Date()
//alert(hoy)
//calculo la fecha que recibo
//La descompongo en un array
var array_fecha = fecha.split("/")
//si el array no tiene tres partes, la fecha es incorrecta
if (array_fecha.length!=3)
return false
//compruebo que los ano, mes, dia son correctos
var ano
ano = parseInt(array_fecha[2]);
if (isNaN(ano))
return false
var mes
mes = parseInt(array_fecha[1]);
if (isNaN(mes))
return false
var dia
dia = parseInt(array_fecha[0]);
if (isNaN(dia))
return false

//si el ao de la fecha que recibo solo tiene 2 cifras hay que cambiarlo a 4
if (ano<=99)
ano +=1900
//resto los aos de las dos fechas
edad=hoy.getYear()- ano - 1; //-1 porque no se si ha cumplido aos ya este ao
//si resto los meses y me da menor que 0 entonces no ha cumplido aos. Si da m
ayor si ha cumplido
if (hoy.getMonth() + 1 - mes < 0) //+ 1 porque los meses empiezan en 0
return edad
if (hoy.getMonth() + 1 - mes > 0)
return edad+1
//entonces es que eran iguales. miro los dias
//si resto los dias y me da menor que 0 entonces no ha cumplido aos. Si da ma
yor o igual si ha cumplido
if (hoy.getUTCDate() - dia >= 0)
return edad + 1
return edad
}
/*************************************************************/
// NumeroALetras
// @author Rodolfo Carmona
/*************************************************************/
function Unidades(num){
switch(num)
{
case 1: return
case 2: return
case 3: return
case 4: return
case 5: return
case 6: return
case 7: return
case 8: return
case 9: return
}

"UN";
"DOS";
"TRES";
"CUATRO";
"CINCO";
"SEIS";
"SIETE";
"OCHO";
"NUEVE";

return "";
}
function Decenas(num){
decena = Math.floor(num/10);
unidad = num - (decena * 10);
switch(decena)
{
case 1:
switch(unidad)
{
case 0: return "DIEZ";
case 1: return "ONCE";
case 2: return "DOCE";

case 3: return "TRECE";


case 4: return "CATORCE";
case 5: return "QUINCE";
default: return "DIECI" + Unidades(unidad);
}
case 2:
switch(unidad)
{
case 0: return "VEINTE";
default: return "VEINTI" + Unidades(unidad);
}
case 3: return DecenasY("TREINTA", unidad);
case 4: return DecenasY("CUARENTA", unidad);
case 5: return DecenasY("CINCUENTA", unidad);
case 6: return DecenasY("SESENTA", unidad);
case 7: return DecenasY("SETENTA", unidad);
case 8: return DecenasY("OCHENTA", unidad);
case 9: return DecenasY("NOVENTA", unidad);
case 0: return Unidades(unidad);
}
}//Unidades()
function DecenasY(strSin, numUnidades){
if (numUnidades > 0)
return strSin + " Y " + Unidades(numUnidades)
return strSin;
}//DecenasY()
function Centenas(num){
centenas = Math.floor(num / 100);
decenas = num - (centenas * 100);
switch(centenas)
{
case 1:
if (decenas > 0)
return "CIENTO " + Decenas(decenas);
return "CIEN";
case 2: return "DOSCIENTOS " + Decenas(decenas);
case 3: return "TRESCIENTOS " + Decenas(decenas);
case 4: return "CUATROCIENTOS " + Decenas(decenas);
case 5: return "QUINIENTOS " + Decenas(decenas);
case 6: return "SEISCIENTOS " + Decenas(decenas);
case 7: return "SETECIENTOS " + Decenas(decenas);
case 8: return "OCHOCIENTOS " + Decenas(decenas);
case 9: return "NOVECIENTOS " + Decenas(decenas);
}
return Decenas(decenas);
}//Centenas()
function Seccion(num, divisor, strSingular, strPlural){
cientos = Math.floor(num / divisor)
resto = num - (cientos * divisor)
letras = "";
if (cientos > 0)

if (cientos > 1)
letras = Centenas(cientos) + " " + strPlural;
else
letras = strSingular;
if (resto > 0)
letras += "";
return letras;
}//Seccion()
function Miles(num){
divisor = 1000;
cientos = Math.floor(num / divisor)
resto = num - (cientos * divisor)
strMiles = Seccion(num, divisor, "UN MIL", "MIL");
strCentenas = Centenas(resto);
if(strMiles == "")
return strCentenas;
return strMiles + " " + strCentenas;
//return Seccion(num, divisor, "UN MIL", "MIL") + " " + Centenas(resto);
}//Miles()
function Millones(num){
divisor = 1000000;
cientos = Math.floor(num / divisor)
resto = num - (cientos * divisor)
strMillones = Seccion(num, divisor, "UN MILLON", "MILLONES");
strMiles = Miles(resto);
if(strMillones == "")
return strMiles;
return strMillones + " " + strMiles;
//return Seccion(num, divisor, "UN MILLON", "MILLONES") + " " + Miles(resto);
}//Millones()
function NumeroALetras(num){
var data = {
numero: num,
enteros: Math.floor(num),
centavos: (((Math.round(num * 100)) - (Math.floor(num) * 100))),
letrasCentavos: "",
letrasMonedaPlural: "CORDOBAS",
letrasMonedaSingular: "CORDOBA"
};
if (data.centavos > 0)
data.letrasCentavos = "CON " + data.centavos + "/100";
if(data.enteros == 0)
return "CERO " + data.letrasMonedaPlural + " " + data.letrasCentavos;
if (data.enteros == 1)
return Millones(data.enteros) + " " + data.letrasMonedaSingular + " " + data

.letrasCentavos;
else
return Millones(data.enteros) + " " + data.letrasMonedaPlural + " " + data.l
etrasCentavos;
}//NumeroALetras()
-----<!doctype html>
<html>
<head>
<title>Numero a letras</title>
<script type="text/javascript">
// Convert numbers to words
// copyright 25th July 2006, by Stephen Chapman http://javascript.about.
com
// permission to use this Javascript on your web page is granted
// provided that all of the code (including this copyright notice) is
// used exactly as shown (you can change the numbering system if you wis
h)
// American Numbering System
var th = ['','thousand','million', 'billion','trillion'];
// uncomment this line for English Number System
// var th = ['','thousand','million', 'milliard','billion'];
var dg = ['zero','one','two','three','four', 'five','six','seven','eight
','nine'];
var tn = ['ten','eleven','twelve','thirteen', 'fourteen','fifteen','sixt
een', 'seventeen','eighteen','nineteen'];
var tw = ['twenty','thirty','forty','fifty', 'sixty','seventy','eighty',
'ninety'];
function toWords(s){s = s.toString(); s = s.replace(/[\, ]/g,''); if (s
!= parseFloat(s)) return 'not a number'; var x = s.indexOf('.'); if (x == -1) x
= s.length; if (x > 15) return 'too big'; var n = s.split(''); var str = ''; var
sk = 0; for (var i=0; i < x; i++) {if ((x-i)%3==2) {if (n[i] == '1') {str += tn
[Number(n[i+1])] + ' '; i++; sk=1;} else if (n[i]!=0) {str += tw[n[i]-2] + ' ';s
k=1;}} else if (n[i]!=0) {str += dg[n[i]] +' '; if ((x-i)%3==0) str += 'hundred
';sk=1;} if ((x-i)%3==1) {if (sk) str += th[(x-i-1)/3] + ' ';sk=0;}} if (x != s.
length) {var y = s.length; str += 'point '; for (var i=x+1; i<y; i++) str += dg[
n[i]] +' ';} return str.replace(/\s+/g,' ');}
</script>
<script type="text/javascript">
function mostrarNumero(elem){
var texto = toWords(elem.value);
document.getElementById('mostrarAca').innerHTML = texto;
}
</script>
</head>
<body>
<input type="text" onkeyup="mostrarNumero(this)" />
<p id="mostrarAca"></p>
</body>
</html>
---<html>
<script>

var o=new Array("diez", "once", "doce", "trece", "catorce", "quince", "diecisis",


"diecisiete", "dieciocho", "diecinueve", "veinte", "veintiuno", "veintids", "vei
ntitrs", "veinticuatro", "veinticinco", "veintisis", "veintisiete", "veintiocho",
"veintinueve");
var u=new Array("cero", "uno", "dos", "tres", "cuatro", "cinco", "seis", "siete"
, "ocho", "nueve");
var d=new Array("", "", "", "treinta", "cuarenta", "cincuenta", "sesenta", "sete
nta", "ochenta", "noventa");
var c=new Array("", "ciento", "doscientos", "trescientos", "cuatrocientos", "qui
nientos", "seiscientos", "setecientos", "ochocientos", "novecientos");
function nn(n)
{
var n=parseFloat(n).toFixed(2); /*se limita a dos decimales, no saba que exista
toFixed() :)*/
var p=n.toString().substring(n.toString().indexOf(".")+1); /*decimales*/
var m=n.toString().substring(0,n.toString().indexOf(".")); /*nmero sin decimale
s*/
var m=parseFloat(m).toString().split("").reverse(); /*tampoco que reverse() ex
ista :D*/
var t="";
/*Se analiza cada 3 dgitos*/
for (var i=0; i<m.length; i+=3)
{
var x=t;
/*formamos un nmero de 2 dgitos*/
var b=m[i+1]!=undefined?parseFloat(m[i+1].toString()+m[i].toString()):parseF
loat(m[i].toString());
/*analizamos el 3 dgito*/
t=m[i+2]!=undefined?(c[m[i+2]]+" "):"";
t+=b<10?u[b]:(b<30?o[b-10]:(d[m[i+1]]+(m[i]=='0'?"":(" y "+u[m[i]]))));
t=t=="ciento cero"?"cien":t;
if (2<i&&i<6)
t=t=="uno"?"mil ":(t.replace("uno","un")+" mil ");
if (5<i&&i<9)
t=t=="uno"?"un milln ":(t.replace("uno","un")+" millones ");
t+=x;
//t=i<3?t:(i<6?((t=="uno"?"mil ":(t+" mil "))+x):((t=="uno"?"un milln ":(t+"
millones "))+x));
}
t+=" con "+p+"/100";
/*correcciones*/
t=t.replace(" "," ");
t=t.replace(" cero","");
//t=t.replace("ciento y","cien y");
//alert("Numero: "+n+"\nN Dgitos: "+m.length+"\nDgitos: "+m+"\nDecimales: "+p+"\n
t: "+t);
//document.getElementById("esc").value=t;
return t;
}
function st()
{
var t="<table><tr><th>nmero</th><th>escrito</th></tr>";
for (var i=2; i<1000000; i+=892.45)
t+="<tr><td>"+i.toFixed(2)+"</td><td>"+nn(i)+"</td></tr>";
t+="</table>";
document.getElementById('out').innerHTML=t;

}
window.onload=st;
</script>
<body>
<!--numero: <input id="num"><input type="button" value="C" onclick="nn(document.
getElementById('num').value);"><br>
escrito: <input style="width: 40em;" id="esc">-->
<div id="out"></div>
</body>
</html>

Anda mungkin juga menyukai