JavaScript - les comparto este codigo de redondear un numero de 2 a 4 decimales

 
Vista:
sin imagen de perfil

les comparto este codigo de redondear un numero de 2 a 4 decimales

Publicado por anonymous (50 intervenciones) el 31/05/2016 07:02:10
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
<!DOCTYPE html>
      <html>
        <head>
          <meta  content="text/html; charset=utf-8"  http-equiv="content-type">
          <title>triangulos</title>
     </head>
     <script  language="JavaScript">
     function re2nd() {
       var y = document.getElementById("rend").options;
       var x = document.getElementById("rend").selectedIndex;
       switch (y[x].index){
       case 0:
       document.calc.a.value=(Math.round(document.calc.a.value * 100) / 100);
	   break;
       case 1:
       document.calc.a.value=(Math.round(document.calc.a.value * 1000) / 1000);
       break;
       case 2:
       document.calc.a.value=(Math.round(document.calc.a.value * 10000) / 10000);
       break;
    }}
</script>
        <body>
          <form  name="calc">
            <select  id="rend" onChange="re2nd()">
              <option>2 decimales</option>
              <option>3 decimales</option>
              <option>4 decimales</option>
            </select><p>pon el numero y ahu mismo se redondeara</p>
            N° a redondear:<br><input name="a" autocomplete="off">
            <input  value="Calcular"  onclick="re2nd()"  type="button">
          </form>
       </body>
</html>
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder