MCPcopy Create free account
hub / github.com/TheAlgorithms/Python / kelvin_to_celsius

Function kelvin_to_celsius

conversions/temperature_conversions.py:170–191  ·  view source on GitHub ↗

Convert a given value from Kelvin to Celsius and round it to 2 decimal places. Wikipedia reference: https://en.wikipedia.org/wiki/Kelvin Wikipedia reference: https://en.wikipedia.org/wiki/Celsius >>> kelvin_to_celsius(273.354, 3) 0.204 >>> kelvin_to_celsius(273.354, 0)

(kelvin: float, ndigits: int = 2)

Source from the content-addressed store, hash-verified

168
169
170def kelvin_to_celsius(kelvin: float, ndigits: int = 2) -> float:
171 """
172 Convert a given value from Kelvin to Celsius and round it to 2 decimal places.
173 Wikipedia reference: https://en.wikipedia.org/wiki/Kelvin
174 Wikipedia reference: https://en.wikipedia.org/wiki/Celsius
175
176 >>> kelvin_to_celsius(273.354, 3)
177 0.204
178 >>> kelvin_to_celsius(273.354, 0)
179 0.0
180 >>> kelvin_to_celsius(273.15)
181 0.0
182 >>> kelvin_to_celsius(300)
183 26.85
184 >>> kelvin_to_celsius("315.5")
185 42.35
186 >>> kelvin_to_celsius("kelvin")
187 Traceback (most recent call last):
188 ...
189 ValueError: could not convert string to float: 'kelvin'
190 """
191 return round(float(kelvin) - 273.15, ndigits)
192
193
194def kelvin_to_fahrenheit(kelvin: float, ndigits: int = 2) -> float:

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected