Skip to content
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions conversions/celsius_to_fahrenheit.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
""" Convert temperature from Celsius to Fahrenheit """


def celsius_to_fahrenheit(celsius: float) -> float:
"""
Convert a given value from Celsius to Fahrenheit and round it to 2 decimal places.

>>> print(celsius_to_fahrenheit(-40))
-40.0
>>> print(celsius_to_fahrenheit(-20))
-4.0
>>> print(celsius_to_fahrenheit(0))
32.0
>>> print(celsius_to_fahrenheit(20))
68.0
>>> print(celsius_to_fahrenheit(40))
104.0
>>> print(celsius_to_fahrenheit("celsius"))
Traceback (most recent call last):
...
ValueError: could not convert string to float: 'celsius'
"""

celsius = float(celsius)
return round((celsius * 9 / 5) + 32, 2)


if __name__ == "__main__":
import doctest
doctest.testmod()
31 changes: 31 additions & 0 deletions conversions/fahrenheit_to_celsius.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
""" Convert temperature from Fahrenheit to Celsius """


def fahrenheit_to_celsius(fahrenheit: float) -> float:
"""
Convert a given value from Fahrenheit to Celsius and round it to 2 decimal places.

>>> print(fahrenheit_to_celsius(0))
-17.78
>>> print(fahrenheit_to_celsius(20))
-6.67
>>> print(fahrenheit_to_celsius(40))
4.44
>>> print(fahrenheit_to_celsius(60))
15.56
>>> print(fahrenheit_to_celsius(80))
26.67
>>> print(fahrenheit_to_celsius(100))
37.78
>>> print(fahrenheit_to_celsius("fahrenheit"))
Traceback (most recent call last):
...
ValueError: could not convert string to float: 'fahrenheit'
"""
fahrenheit = float(fahrenheit)
return round((fahrenheit - 32) * 5 / 9, 2)


if __name__ == "__main__":
import doctest
doctest.testmod()