Skip to content

Commit 6574b43

Browse files
karimzakir02cclauss
authored andcommitted
Celsius to Fahrenheit Conversions (TheAlgorithms#2188)
* added conversions between celsius and fahrenheit * Renamed celsius_to_fahrenheit.py * Fixed spelling issues * modified file to fit the 88-character limit * added changes to pass the travis-ci test * further changed the files to pass the travis-ci test * further changed the files to pass the travis-ci test * Shortened conversions/fahrenheit_to_celsius.py Co-authored-by: Christian Clauss <[email protected]> * Type hints added to conversions/fahrenheit_to_celsius.py Co-authored-by: Christian Clauss <[email protected]> * changed the code to let the caller do the printing * addressed the changes made on github * Added Kelvin conversions and put temperature functions in a single file * Removed whitespace from a blank line * Update temperature_conversions.py Co-authored-by: Christian Clauss <[email protected]>
1 parent 459f59a commit 6574b43

File tree

1 file changed

+88
-0
lines changed

1 file changed

+88
-0
lines changed
+88
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
""" Convert between different units of temperature """
2+
3+
4+
def celsius_to_fahrenheit(celsius: float) -> float:
5+
"""
6+
Convert a given value from Celsius to Fahrenheit and round it to 2 decimal places.
7+
8+
>>> celsius_to_fahrenheit(-40.0)
9+
-40.0
10+
>>> celsius_to_fahrenheit(-20.0)
11+
-4.0
12+
>>> celsius_to_fahrenheit(0)
13+
32.0
14+
>>> celsius_to_fahrenheit(20)
15+
68.0
16+
>>> celsius_to_fahrenheit("40")
17+
104.0
18+
>>> celsius_to_fahrenheit("celsius")
19+
Traceback (most recent call last):
20+
...
21+
ValueError: could not convert string to float: 'celsius'
22+
"""
23+
return round((float(celsius) * 9 / 5) + 32, 2)
24+
25+
26+
def fahrenheit_to_celsius(fahrenheit: float) -> float:
27+
"""
28+
Convert a given value from Fahrenheit to Celsius and round it to 2 decimal places.
29+
30+
>>> fahrenheit_to_celsius(0)
31+
-17.78
32+
>>> fahrenheit_to_celsius(20.0)
33+
-6.67
34+
>>> fahrenheit_to_celsius(40.0)
35+
4.44
36+
>>> fahrenheit_to_celsius(60)
37+
15.56
38+
>>> fahrenheit_to_celsius(80)
39+
26.67
40+
>>> fahrenheit_to_celsius("100")
41+
37.78
42+
>>> fahrenheit_to_celsius("fahrenheit")
43+
Traceback (most recent call last):
44+
...
45+
ValueError: could not convert string to float: 'fahrenheit'
46+
"""
47+
return round((float(fahrenheit) - 32) * 5 / 9, 2)
48+
49+
50+
def celsius_to_kelvin(celsius: float) -> float:
51+
"""
52+
Convert a given value from Celsius to Kelvin and round it to 2 decimal places.
53+
54+
>>> celsius_to_kelvin(0)
55+
273.15
56+
>>> celsius_to_kelvin(20.0)
57+
293.15
58+
>>> celsius_to_kelvin("40")
59+
313.15
60+
>>> celsius_to_kelvin("celsius")
61+
Traceback (most recent call last):
62+
...
63+
ValueError: could not convert string to float: 'celsius'
64+
"""
65+
return round(float(celsius) + 273.15, 2)
66+
67+
68+
def kelvin_to_celsius(kelvin: float) -> float:
69+
"""
70+
Convert a given value from Kelvin to Celsius and round it to 2 decimal places.
71+
72+
>>> kelvin_to_celsius(273.15)
73+
0.0
74+
>>> kelvin_to_celsius(300)
75+
26.85
76+
>>> kelvin_to_celsius("315.5")
77+
42.35
78+
>>> kelvin_to_celsius("kelvin")
79+
Traceback (most recent call last):
80+
...
81+
ValueError: could not convert string to float: 'kelvin'
82+
"""
83+
return round(float(kelvin) - 273.15, 2)
84+
85+
86+
if __name__ == "__main__":
87+
import doctest
88+
doctest.testmod()

0 commit comments

Comments
 (0)