Comunidad de diseño web y desarrollo en internet online

redondear número con javascript

Citar            
MensajeEscrito el 23 Oct 2006 04:15 pm
Estimados: tengo el siguiente código en javascript. Todo funciona bien salvo que necesito que el número del dividendo en pesos se exprese redondeado a dos decimales. ¿Que me falta?

Código :

<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<script>
function monto(valor){if (valor!='NaN') return 357-valor}
function dividendo(valor){if (valor!='NaN') return (357-valor)*0.008192}
</script>

<script language=Javascript>

function Borrar () {
window.document.formulario.reset()
}

</script>

<form name="formulario">
  <table width="486" border="0" cellspacing="0">
    <tr> 
      <td colspan="3">&nbsp;</td>
    </tr>
    <tr> 
      <td width="207" height="16"> 
        <div align="center"><font face="Arial, Helvetica, sans-serif" size="2">Indica 
          tu ahorro (desde 30 UF) </font> </div>
      </td>
      <td width="136" height="16"> 
        <div align="right"> 
          <input type="text" name="ahorro" size="3"
      onClick="this.form.cantida.value=''">
          <font size="2" face="Arial, Helvetica, sans-serif">UF</font> </div>
      </td>
      <td width="181" height="16"> 
        <div align="right"> 
          <input name="boton_limpiar" value="Limpiar ahorro" type="BUTTON" onClick="javascript:Borrar();">
        </div>
      </td>
    </tr>
    <tr> 
      <td colspan="3">&nbsp;</td>
    </tr>
    <tr> 
      <td width="207"> 
        <div align="center"> 
          <input type="button" value="Calcular monto cr&eacute;dito" name="boton_credito"
      onClick="this.form.out_credito.value=monto(this.form.ahorro.value)">
        </div>
      </td>
      <td width="136"> 
        <div align="right"> 
          <input type="text" name="out_credito" size="3"
      onClick="this.form.ahorro.value=''">
          <font size="2" face="Arial, Helvetica, sans-serif">UF</font></div>
      </td>
      <td width="181">&nbsp; </td>
    </tr>
    <tr> 
      <td colspan="3">&nbsp;</td>
    </tr>
    <tr> 
      <td width="207"> 
        <div align="center"> 
          <input type="button" name="valor_dividendo" value="Calcular valor dividendo"
      onClick="this.form.out_dividendo.value=dividendo(this.form.ahorro.value);this.form.out_dividendopesos.value=dividendo(this.form.ahorro.value)*18500">
        </div>
      </td>
      <td width="136"> 
        <div align="right"> 
          <input type="text" name="out_dividendo" size="10"
      onClick="this.form.ahorro.value=''">
          <font size="2" face="Arial, Helvetica, sans-serif"> UF</font></div>
      </td>
      <td width="181"> 
        <div align="right">$ 
          <input type="text" name="out_dividendopesos" size="10"
      onClick="this.form.ahorro.value=''">
        </div>
      </td>
    </tr>
    <tr> 
      <td width="207">&nbsp;</td>
      <td width="136">&nbsp;</td>
      <td width="181">&nbsp;</td>
    </tr>
    <tr> 
      <td width="207"> 
        <div align="center"><font face="Arial, Helvetica, sans-serif" size="2">Valor 
          casa</font></div>
      </td>
      <td width="136"> 
        <div align="right"><font face="Arial, Helvetica, sans-serif" size="2">620 
          UF</font></div>
      </td>
      <td width="181">&nbsp;</td>
    </tr>
    <tr>
      <td colspan="3">&nbsp;</td>
    </tr>
    <tr> 
      <td colspan="3"> 
        <div align="center"><font size="1" face="Arial, Helvetica, sans-serif">C&aacute;lculos 
          de cr&eacute;dito hipotecario y dividendo incluyen gastos operacionales 
          (17 UF)</font></div>
        <div align="right"></div>
      </td>
    </tr>
  </table>
  <p>&nbsp;</p>
  <p>&nbsp;</p>
  <p>&nbsp; </p>
  <p>&nbsp; </p>
  <p>&nbsp; </p>
</form>

</body>
</html>


Desde ya gracias por la ayuda. :)

Por gatricio

45 de clabLevel



 

msie
Citar            
MensajeEscrito el 23 Oct 2006 04:25 pm
sería algo así como

Código :

 redondeado=Math.round(importe*100)/100
creo, multimicas la cantidad por 100 y así coges tambien los centimos, al redondear eliminas toda la parte decimal que puedas tener ahora mismo y al volver a dividir por 100 le metes dos decimales (que son los centimos, perdon, centavos ^^) espero esté bien, lo hize aquí, sin probar....

Por Yranac

421 de clabLevel



Genero:Masculino  

Lugo, Galicia

firefox
Citar            
MensajeEscrito el 23 Oct 2006 04:41 pm
OK, gracias pero donde se incluiría el código que dices ??

Por gatricio

45 de clabLevel



 

msie
Citar            
MensajeEscrito el 26 Oct 2006 07:39 pm
Please que alguien me ayude... gracias estimados. :)

Por gatricio

45 de clabLevel



 

msie
Citar            
MensajeEscrito el 27 Oct 2006 08:18 am

gatricio escribió:

OK, gracias pero donde se incluiría el código que dices ??
Justo antes del return que te devuelve el precio final (es que no lo tengo muy claro en tu código ^^

Por Yranac

421 de clabLevel



Genero:Masculino  

Lugo, Galicia

firefox

 

Cristalab BabyBlue v4 + V4 © 2011 Cristalab
Powered by ClabEngines v4, HTML5, love and ponies.