代码示例:(标识:eg_js_math_celsius_to_fahrenheit)
<!DOCTYPE html>
<html>
<body>

<h2>JavaScript 摄氏度转华氏度</h2>

<p>在下面的输入字段之一中插入一个数字:</p>

<p><input id="c" onkeyup="convert('C')"> 摄氏度</p>

<p><input id="f" onkeyup="convert('F')"> 华氏度</p> 

<p>请注意,使用了 <b>Math.round()</b> 方法,因此结果将作为整数返回。</p>

<script>
function convert(degree) {
  var x;
  if (degree == "C") {
    x = document.getElementById("c").value * 9 / 5 + 32;
    document.getElementById("f").value = Math.round(x);
  } else {
    x = (document.getElementById("f").value -32) * 5 / 9;
    document.getElementById("c").value = Math.round(x);
  }
}
</script>

</body>
</html>
运行结果: