• Viernes 19 de Abril de 2024, 09:52

Autor Tema:  Calcular Promedio  (Leído 9705 veces)

mcckas

  • Nuevo Miembro
  • *
  • Mensajes: 1
    • Ver Perfil
Calcular Promedio
« en: Miércoles 9 de Marzo de 2005, 19:14 »
0
estoy tratando de crear un formulario que me pueda calcular el promedio de dos notas, asta el momento lo he logrado pero aparece con dos decimales y yo lo quiero con uno.
la idea es que si da 6.75 lo aprocxime a 6.8 etc. mis codigos son los sigientes;


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title></title>
<style type="text/css">
<!--
.Estilo1 {
   font-family: Arial, Helvetica, sans-serif;
   font-size: 12px;
}
.Estilo2 {color: #FFFFFF}
-->
</style>
</head>
<p align="center">
<script language="JavaScript" type="text/javascript">
function dp(price)
{
   string = "" + price;
   number = string.length - string.indexOf('.');
   if (string.indexOf('.') == -1)
      return string + '.0';
   if (number == 1)
      return string + '0';
   if (number == 1)
      return string + '0';
   if (number > 2)
      return string.substring(0,string.length-number+4);
return string;
}
function calculate5()
{
dp (1.0)* document.calcform5.quantity15.value
dp (1.0)* document.calcform5.quantity25.value
document.calcform5.subtotal5.value = dp((eval(document.calcform5.quantity15.value) + eval(document.calcform5.quantity25.value))/2)
}
</script>

<style type="text/css">
<!--
.Estilo4 {font-size: 12px; }
-->
</style>
</HEAD>
</p>
<body>
<form name="calcform5" class="Estilo1" id="calcform5">
  <table width="681" border="1" align="center" bordercolor="#000000">
    <tbody>
      <tr>
        <td width="597"><p> </p>
        </td>
        <td width="68"><select name="quantity15" size="1" onchange="calculate5()" type="text">
            <option>selec...</option>
            <option value="7">7.0</option>
            <option value="6.5">6.5</option>
            <option value="6">6.0</option>
            <option value="5.5">5.5</option>
            <option value="5">5.0</option>
            <option value="4.5">4.5</option>
            <option value="4">4.0</option>
            <option value="3.5">3.5</option>
            <option value="3">3.0</option>
            <option value="2">2.0</option>
            <option value="1">1.0</option>
        </select></td>
      </tr>
      <tr>
        <td> </td>
        <td><select name="quantity25" size="1" onchange="calculate5()" type="text">
            <option>selec...</option>
            <option value="7">7.0</option>
            <option value="6.5">6.5</option>
            <option value="6">6.0</option>
            <option value="5.5">5.5</option>
            <option value="5">5.0</option>
            <option value="4.5">4.5</option>
            <option value="4">4.0</option>
            <option value="3.5">3.5</option>
            <option value="3">3.0</option>
            <option value="2">2.0</option>
            <option value="1">1.0</option>
        </select></td>
      </tr>
      <tr>
        <td><div align="right"><strong>NOTA FINAL </strong></div></td>
        <td><input name="subtotal5" id="subtotal5" value="0.0" size="7" /></td>
      </tr>
    </tbody>
  </table>
</form>
</body>
</p>
</html>

Sasi

  • Nuevo Miembro
  • *
  • Mensajes: 16
    • Ver Perfil
Re: Calcular Promedio
« Respuesta #1 en: Jueves 14 de Abril de 2005, 16:19 »
0
Si he entendido bien, quieres:
Si la media es 6.25 dejarlo en 6.3; te he escrito una funcioncilla que hace la media de dos números y la redondea a un decimal: si la media es 6.25 te devolverá 6.3, si es 6.21 te devolvera 6.2 (se que este caso no se dará nunca en tu script, pero bueno)

function mediayredondea(a,B){
   c=((a+B)/2)*10;
   d=Math.floor©;//floor(6.5)=6;
   e=(c-d);//conseguimos el sequndo decimal
   if(e>=0.5)
        return((d+1)/10);// pués incrementar
   else
        return(d/10);//no incrementar      
}
Sasi guztien gainetik, hodei guztien azpitik.
Por encima de todas las zarzas, por debajo de todas las nubes.

Sasi

  • Nuevo Miembro
  • *
  • Mensajes: 16
    • Ver Perfil
Re: Calcular Promedio
« Respuesta #2 en: Jueves 14 de Abril de 2005, 16:21 »
0
function mediayredondea(a,b){
   c=((a+b)/2)*10;
   d=Math.floor©;//floor(6.5)=6;
   e=(c-d);//conseguimos el sequndo decimal
   if(e>=0.5)
        return((d+1)/10);// pués incrementar
   else
        return(d/10);//no incrementar      
}
Sasi guztien gainetik, hodei guztien azpitik.
Por encima de todas las zarzas, por debajo de todas las nubes.

Altareum

  • Miembro MUY activo
  • ***
  • Mensajes: 327
    • Ver Perfil
    • http://www.avian.com.ar
Re: Calcular Promedio
« Respuesta #3 en: Lunes 18 de Abril de 2005, 19:18 »
0
Pues... podés usar la función Round de javascript. Ejemplo:

alert(Math.round(num*10)/10);

si num <= 6.74 -> devuelve 6.7
si num >= 6.75 -> devuelve 6.8

Altareum.
El postre!, el postre!, el postre!, el postre!, el postre!, el postre!.....
 | Klinka | Kiree | Avian |