Length and weight conversions multiply by a constant, so people assume temperature works the same way. It does not, and the bug is easy to ship.
Why a single multiplier fails
Celsius and Fahrenheit have different zero points, so you need an offset as well as a scale. Kelvin shares the Celsius scale but starts at absolute zero.
const cToF = (c) => c * 9 / 5 + 32;
const fToC = (f) => (f - 32) * 5 / 9;
const cToK = (c) => c + 273.15;
const kToC = (k) => k - 273.15;
Temperature differences are different
A change of 10 degrees Celsius is a change of 18 degrees Fahrenheit, because a difference ignores the offset. If you convert a difference with the full formula you will be off by 32.
const deltaCToF = (dc) => dc * 9 / 5;
Guard the physical limit
Nothing can be colder than absolute zero, so reject Kelvin values below 0, Celsius below -273.15 and Fahrenheit below -459.67.
Round for display only
Keep full precision while computing. Round at the end, usually to one decimal place for weather and two for science.
Test known points
- 0 C = 32 F = 273.15 K
- 100 C = 212 F = 373.15 K
- -40 C = -40 F (the scales meet)
Online converters
I keep free converters with these formulas shown on the page: https://unitconverter.network/celsius-to-kelvin/ and https://unitconverter.network/fahrenheit-to-celsius/ .
Top comments (0)