Switch to full style
JavaScript code examples
Post a reply

temperature transformer between different types

Mon Sep 12, 2011 9:34 pm

temperature transformer between Celsius,Kelvin,Fahrenheit

Code:

<html>
<
title>temperature transformer</title>
<
body>
<
script language="JavaScript" type="text/javascript">
function converttemperature(temperature, typeScale){
     
var Celsius
, Kelvin, Fahrenheit;
     
switch 
(typeScale){
 case "F":
  Fahrenheit = temperature;
  if (temperature){
   Celsius = ((5/9) * (temperature-32));
   Kelvin = ((5/9)*(temperature-32) + 273)
  }else{    
   Celsius 
= "";
   Kelvin = "";
  }
  Kelvin = Kelvin.toFixed(2)
  Celsius = Celsius.toFixed(2)
 break;
     case "K":
  Kelvin = temperature;
   if (temperature){
    Celsius = Kelvin - 273;
    Fahrenheit = ((Celsius * 9/5) + 32)
   }else{
    Fahrenheit = '';
    Kelvin = '';
   }
  Fahrenheit = Fahrenheit.toFixed(2)
  Celsius = Celsius.toFixed(2)
 break;
 case "C":
  Celsius = temperature;
  if (temperature){
   Fahrenheit = ((temperature * 9/5) + 32)
   Kelvin = ((5/9)*(Fahrenheit-32) + 273)
  }else{
   Fahrenheit = '';
   Kelvin = '';
  }
   Kelvin = Kelvin.toFixed(2)
   Fahrenheit = Fahrenheit.toFixed(2)
  break;
    


}

 document.getElementById("kelvinValue").value = Kelvin
 document
.getElementById("celsiusValue").value = Celsius
 document
.getElementById("fahrenheitValue").value = Fahrenheit

}
</script>

<form name="temperature_input">

Celsius: <input type="text" id="celsiusValue" name="celsius" style=" width: 50px;" 
onkeyup="converttemperature(this.value, 'C')">
<br>
Fahrenheit: <input type="text" id="fahrenheitValue" name="fahrenheit" style=" width: 50px;" 
onkeyup="converttemperature(this.value, 'F')"   onchange="converttemperature(this.value, 'F')" >
<br>
Kelvin: <input type="text" id="kelvinValue" name="kelvin" style=" width: 50px;" 
onkeyup="converttemperature(this.value, 'K')">
</form>
</body>

</html>




Post a reply
  Related Posts  to : temperature transformer between different types
 TEMPERATURE AND HEAT CONTROL Assembly     -  
 Types of Registers     -  
 Types of Pointers in C++     -  
 assembler directives types     -  
 java data types     -  
 What are the types of jdbc drivers.?     -  
 pointers to derived types     -  
 Get Registered File Types and Their Associated Icons in C#     -  
 lesson9: XSD Complex Types Indicators     -  
 MYSQL TEXT field types lengths     -  

Topic Tags

JavaScript Functions