Temperature Converter

Easy~10 min

Given a temperature value, its unit ("C", "F", or "K"), and a target unit, convert the temperature and round the result to 2 decimal places.

Conversion formulas:

  • Celsius to Fahrenheit: F = C * 9/5 + 32
  • Celsius to Kelvin: K = C + 273.15
  • Fahrenheit to Celsius: C = (F - 32) * 5/9
  • Fahrenheit to Kelvin: K = (F - 32) * 5/9 + 273.15
  • Kelvin to Celsius: C = K - 273.15
  • Kelvin to Fahrenheit: F = (K - 273.15) * 9/5 + 32

If the source and target units are the same, return the input value rounded to 2 decimal places.

Examples

Example 1
Input: value = 100, fromUnit = "C", toUnit = "F"
Output: 212.0
Explanation: 100 * 9/5 + 32 = 212.0
Example 2
Input: value = 32, fromUnit = "F", toUnit = "C"
Output: 0.0
Explanation: (32 - 32) * 5/9 = 0.0
Example 3
Input: value = 0, fromUnit = "C", toUnit = "K"
Output: 273.15
Explanation: 0 + 273.15 = 273.15

Constraints

  • -1000 <= value <= 1000
  • fromUnit and toUnit are one of "C", "F", or "K"
  • Result should be rounded to 2 decimal places
Code
Ctrl+EnterRun|Ctrl+⇧+EnterSubmit
Output

Run your code to see results

Use Cmd+Enter to run