Trabalhar com temperaturas é uma tarefa simples, mas não tão simples quanto se você já tiver um algoritmo bacana que faz isto pra você, né? 😛
Recentemente montei uma pequena rotina que converte automaticamente as seguintes temperaturas : Celsius , Kelvin , e Fareireiewmtewf (Farenheit ).
Veja o código:
<!DOCTYPE html>
<html lang="pt-br">
<head>
<meta charset="utf-8" />
<title> Conversões </title>
<script type="text/javascript"
src="temperature conversion.js">
</script>
</head>
<body>
<form style="border: 1px double blue; background-color: #DDF;
padding: 4px;text-align:center;"
name="convert">
Fahrenheit: <input type="text"
name="ftemp"
size="7"
onchange="convertTemp('ftoc')"> é o mesmo que Celsius:
<input type="text"
name="ctemp"
size="7"
onchange="convertTemp('ctof')">
é o mesmo que Kelvin:
<input type="text"
name="ktemp"
size="7"
onchange="convertTemp('ktof')">
<br /><br />
<input type="button"
value="limpar"
onclick="clearAll();">
</form>
</body>
</html>
Este HTML nada mais chama a função javascript que converte, a ação é gerada a cada “onchange” do input de texto. Veja o código javascript:
function convertTemp(direction)
{
//instanciando objetos
var fObj = document.convert.ftemp, cObj = document.convert.ctemp, kObj = document.convert.ktemp;
//definindo direções para conversões
if (direction == "ftoc")
{
cObj.value = Math.round((fObj.value - 32) * (5/9));
kObj.value = Math.round((parseInt(cObj.value) + 459.67) * (5/9));
}
else if (direction == "ktof")
{
fObj.value = Math.round((parseInt(cObj.value) * (9/5)) - 459.67);
cObj.value = Math.round((fObj.value - 32) * (5/9));
}
else
{
fObj.value = Math.round((parseInt(cObj.value) * (9/5)) + 32);
kObj.value = Math.round((parseInt(cObj.value) + 273));
}
}
function clearAll()
{
document.convert.ftemp.value="";
document.convert.ctemp.value="";
document.convert.ktemp.value="";
}
No JavaScript eu apenas faço a conversão, utilizando a fórmula padrão de conversões de temperaturas.
Viu só como é simples? Um grande abraço!
