Ver Mensaje Individual
  #149 (permalink)  
Antiguo 03/10/2003, 06:22
Avatar de JavierB
JavierB
Colaborador
 
Fecha de Ingreso: febrero-2002
Ubicación: Madrid
Mensajes: 25.052
Antigüedad: 22 años, 2 meses
Puntos: 772
151.- Controlar el número de decimales de un número.

P: ¿Como puedo mostrar un número con una determinada cantidad de decimales?

R: Con este pequeño código:
Código PHP:
<html>
<
head>
<
script>
Number.prototype.decimal = function (num) {
    
pot Math.pow(10,num);
    return 
parseInt(this pot) / pot;
}
function 
decimal(src) {
    
n=eval(document.getElementById(src).value);
    
alert(n.decimal(3)); //deja 3 decimales
}
</script>
</head>

<body>
<input type="text" id="numero" value="3.1415926">
<input type="button" onClick="decimal('numero')">
</body>
</html> 

Última edición por JavierB; 05/12/2003 a las 13:03