← 返回完整题库
JavaScript简单

编程实现温度转换,已知温度转换的关系式是:华氏度=32+摄氏度×1.8,现在要求输入摄氏度,输出对应的华氏度,小数保留两位

参考答案

function c2f(c) {
  const f = 32 + Number(c) * 1.8;
  return Number(f.toFixed(2));   // 需要字符串时用 f.toFixed(2)
}
c2f(37) // 98.6

注意:

  • 输入校验(非数字返回提示);
  • toFixed 返回字符串,需数值时用 Number/parseFloat;
  • 精度问题可用 Math.round(f * 100) / 100 避免浮点误差。

本题来自 计算机知识面试题库,共 921 道题。