1
1
""" Convert between different units of temperature """
2
2
3
3
4
- def celsius_to_fahrenheit (celsius : float ) -> float :
4
+ def celsius_to_fahrenheit (celsius : float , ndigits : int = 2 ) -> float :
5
5
"""
6
6
Convert a given value from Celsius to Fahrenheit and round it to 2 decimal places.
7
+ Wikipedia reference: https://en.wikipedia.org/wiki/Celsius
8
+ Wikipedia reference: https://en.wikipedia.org/wiki/Fahrenheit
7
9
8
10
>>> celsius_to_fahrenheit(-40.0)
9
11
-40.0
@@ -20,12 +22,54 @@ def celsius_to_fahrenheit(celsius: float) -> float:
20
22
...
21
23
ValueError: could not convert string to float: 'celsius'
22
24
"""
23
- return round ((float (celsius ) * 9 / 5 ) + 32 , 2 )
25
+ return round ((float (celsius ) * 9 / 5 ) + 32 , ndigits )
24
26
25
27
26
- def fahrenheit_to_celsius (fahrenheit : float ) -> float :
28
+ def celsius_to_kelvin (celsius : float , ndigits : int = 2 ) -> float :
29
+ """
30
+ Convert a given value from Celsius to Kelvin and round it to 2 decimal places.
31
+ Wikipedia reference: https://en.wikipedia.org/wiki/Celsius
32
+ Wikipedia reference: https://en.wikipedia.org/wiki/Kelvin
33
+
34
+ >>> celsius_to_kelvin(0)
35
+ 273.15
36
+ >>> celsius_to_kelvin(20.0)
37
+ 293.15
38
+ >>> celsius_to_kelvin("40")
39
+ 313.15
40
+ >>> celsius_to_kelvin("celsius")
41
+ Traceback (most recent call last):
42
+ ...
43
+ ValueError: could not convert string to float: 'celsius'
44
+ """
45
+ return round (float (celsius ) + 273.15 , ndigits )
46
+
47
+
48
+ def celsius_to_rankine (celsius : float , ndigits : int = 2 ) -> float :
49
+ """
50
+ Convert a given value from Celsius to Rankine and round it to 2 decimal places.
51
+ Wikipedia reference: https://en.wikipedia.org/wiki/Celsius
52
+ Wikipedia reference: https://en.wikipedia.org/wiki/Rankine_scale
53
+
54
+ >>> celsius_to_rankine(0)
55
+ 491.67
56
+ >>> celsius_to_rankine(20.0)
57
+ 527.67
58
+ >>> celsius_to_rankine("40")
59
+ 563.67
60
+ >>> celsius_to_rankine("celsius")
61
+ Traceback (most recent call last):
62
+ ...
63
+ ValueError: could not convert string to float: 'celsius'
64
+ """
65
+ return round ((float (celsius ) * 9 / 5 ) + 491.67 , ndigits )
66
+
67
+
68
+ def fahrenheit_to_celsius (fahrenheit : float , ndigits : int = 2 ) -> float :
27
69
"""
28
70
Convert a given value from Fahrenheit to Celsius and round it to 2 decimal places.
71
+ Wikipedia reference: https://en.wikipedia.org/wiki/Fahrenheit
72
+ Wikipedia reference: https://en.wikipedia.org/wiki/Celsius
29
73
30
74
>>> fahrenheit_to_celsius(0)
31
75
-17.78
@@ -44,30 +88,66 @@ def fahrenheit_to_celsius(fahrenheit: float) -> float:
44
88
...
45
89
ValueError: could not convert string to float: 'fahrenheit'
46
90
"""
47
- return round ((float (fahrenheit ) - 32 ) * 5 / 9 , 2 )
91
+ return round ((float (fahrenheit ) - 32 ) * 5 / 9 , ndigits )
48
92
49
93
50
- def celsius_to_kelvin ( celsius : float ) -> float :
94
+ def fahrenheit_to_kelvin ( fahrenheit : float , ndigits : int = 2 ) -> float :
51
95
"""
52
- Convert a given value from Celsius to Kelvin and round it to 2 decimal places.
96
+ Convert a given value from Fahrenheit to Kelvin and round it to 2 decimal places.
97
+ Wikipedia reference: https://en.wikipedia.org/wiki/Fahrenheit
98
+ Wikipedia reference: https://en.wikipedia.org/wiki/Kelvin
53
99
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")
100
+ >>> fahrenheit_to_kelvin(0)
101
+ 255.37
102
+ >>> fahrenheit_to_kelvin(20.0)
103
+ 266.48
104
+ >>> fahrenheit_to_kelvin(40.0)
105
+ 277.59
106
+ >>> fahrenheit_to_kelvin(60)
107
+ 288.71
108
+ >>> fahrenheit_to_kelvin(80)
109
+ 299.82
110
+ >>> fahrenheit_to_kelvin("100")
111
+ 310.93
112
+ >>> fahrenheit_to_kelvin("fahrenheit")
61
113
Traceback (most recent call last):
62
114
...
63
- ValueError: could not convert string to float: 'celsius'
115
+ ValueError: could not convert string to float: 'fahrenheit'
116
+ """
117
+ return round (((float (fahrenheit ) - 32 ) * 5 / 9 ) + 273.15 , ndigits )
118
+
119
+
120
+ def fahrenheit_to_rankine (fahrenheit : float , ndigits : int = 2 ) -> float :
121
+ """
122
+ Convert a given value from Fahrenheit to Rankine and round it to 2 decimal places.
123
+ Wikipedia reference: https://en.wikipedia.org/wiki/Fahrenheit
124
+ Wikipedia reference: https://en.wikipedia.org/wiki/Rankine_scale
125
+
126
+ >>> fahrenheit_to_rankine(0)
127
+ 459.67
128
+ >>> fahrenheit_to_rankine(20.0)
129
+ 479.67
130
+ >>> fahrenheit_to_rankine(40.0)
131
+ 499.67
132
+ >>> fahrenheit_to_rankine(60)
133
+ 519.67
134
+ >>> fahrenheit_to_rankine(80)
135
+ 539.67
136
+ >>> fahrenheit_to_rankine("100")
137
+ 559.67
138
+ >>> fahrenheit_to_rankine("fahrenheit")
139
+ Traceback (most recent call last):
140
+ ...
141
+ ValueError: could not convert string to float: 'fahrenheit'
64
142
"""
65
- return round (float (celsius ) + 273.15 , 2 )
143
+ return round (float (fahrenheit ) + 459.67 , ndigits )
66
144
67
145
68
- def kelvin_to_celsius (kelvin : float ) -> float :
146
+ def kelvin_to_celsius (kelvin : float , ndigits : int = 2 ) -> float :
69
147
"""
70
148
Convert a given value from Kelvin to Celsius and round it to 2 decimal places.
149
+ Wikipedia reference: https://en.wikipedia.org/wiki/Kelvin
150
+ Wikipedia reference: https://en.wikipedia.org/wiki/Celsius
71
151
72
152
>>> kelvin_to_celsius(273.15)
73
153
0.0
@@ -80,9 +160,111 @@ def kelvin_to_celsius(kelvin: float) -> float:
80
160
...
81
161
ValueError: could not convert string to float: 'kelvin'
82
162
"""
83
- return round (float (kelvin ) - 273.15 , 2 )
163
+ return round (float (kelvin ) - 273.15 , ndigits )
164
+
165
+
166
+ def kelvin_to_fahrenheit (kelvin : float , ndigits : int = 2 ) -> float :
167
+ """
168
+ Convert a given value from Kelvin to Fahrenheit and round it to 2 decimal places.
169
+ Wikipedia reference: https://en.wikipedia.org/wiki/Kelvin
170
+ Wikipedia reference: https://en.wikipedia.org/wiki/Fahrenheit
171
+
172
+ >>> kelvin_to_fahrenheit(273.15)
173
+ 32.0
174
+ >>> kelvin_to_fahrenheit(300)
175
+ 80.33
176
+ >>> kelvin_to_fahrenheit("315.5")
177
+ 108.23
178
+ >>> kelvin_to_fahrenheit("kelvin")
179
+ Traceback (most recent call last):
180
+ ...
181
+ ValueError: could not convert string to float: 'kelvin'
182
+ """
183
+ return round (((float (kelvin ) - 273.15 ) * 9 / 5 ) + 32 , ndigits )
184
+
185
+
186
+ def kelvin_to_rankine (kelvin : float , ndigits : int = 2 ) -> float :
187
+ """
188
+ Convert a given value from Kelvin to Rankine and round it to 2 decimal places.
189
+ Wikipedia reference: https://en.wikipedia.org/wiki/Kelvin
190
+ Wikipedia reference: https://en.wikipedia.org/wiki/Rankine_scale
191
+
192
+ >>> kelvin_to_rankine(0)
193
+ 0.0
194
+ >>> kelvin_to_rankine(20.0)
195
+ 36.0
196
+ >>> kelvin_to_rankine("40")
197
+ 72.0
198
+ >>> kelvin_to_rankine("kelvin")
199
+ Traceback (most recent call last):
200
+ ...
201
+ ValueError: could not convert string to float: 'kelvin'
202
+ """
203
+ return round ((float (kelvin ) * 9 / 5 ), ndigits )
204
+
205
+
206
+ def rankine_to_celsius (rankine : float , ndigits : int = 2 ) -> float :
207
+ """
208
+ Convert a given value from Rankine to Celsius and round it to 2 decimal places.
209
+ Wikipedia reference: https://en.wikipedia.org/wiki/Rankine_scale
210
+ Wikipedia reference: https://en.wikipedia.org/wiki/Celsius
211
+
212
+ >>> rankine_to_celsius(273.15)
213
+ -121.4
214
+ >>> rankine_to_celsius(300)
215
+ -106.48
216
+ >>> rankine_to_celsius("315.5")
217
+ -97.87
218
+ >>> rankine_to_celsius("rankine")
219
+ Traceback (most recent call last):
220
+ ...
221
+ ValueError: could not convert string to float: 'rankine'
222
+ """
223
+ return round ((float (rankine ) - 491.67 ) * 5 / 9 , ndigits )
224
+
225
+
226
+ def rankine_to_fahrenheit (rankine : float , ndigits : int = 2 ) -> float :
227
+ """
228
+ Convert a given value from Rankine to Fahrenheit and round it to 2 decimal places.
229
+ Wikipedia reference: https://en.wikipedia.org/wiki/Rankine_scale
230
+ Wikipedia reference: https://en.wikipedia.org/wiki/Fahrenheit
231
+
232
+ >>> rankine_to_fahrenheit(273.15)
233
+ -186.52
234
+ >>> rankine_to_fahrenheit(300)
235
+ -159.67
236
+ >>> rankine_to_fahrenheit("315.5")
237
+ -144.17
238
+ >>> rankine_to_fahrenheit("rankine")
239
+ Traceback (most recent call last):
240
+ ...
241
+ ValueError: could not convert string to float: 'rankine'
242
+ """
243
+ return round (float (rankine ) - 459.67 , ndigits )
244
+
245
+
246
+ def rankine_to_kelvin (rankine : float , ndigits : int = 2 ) -> float :
247
+ """
248
+ Convert a given value from Rankine to Kelvin and round it to 2 decimal places.
249
+ Wikipedia reference: https://en.wikipedia.org/wiki/Rankine_scale
250
+ Wikipedia reference: https://en.wikipedia.org/wiki/Kelvin
251
+
252
+ >>> rankine_to_kelvin(0)
253
+ 0.0
254
+ >>> rankine_to_kelvin(20.0)
255
+ 11.11
256
+ >>> rankine_to_kelvin("40")
257
+ 22.22
258
+ >>> rankine_to_kelvin("rankine")
259
+ Traceback (most recent call last):
260
+ ...
261
+ ValueError: could not convert string to float: 'rankine'
262
+ """
263
+ return round ((float (rankine ) * 5 / 9 ), ndigits )
84
264
85
265
86
266
if __name__ == "__main__" :
267
+
87
268
import doctest
269
+
88
270
doctest .testmod ()
0 commit comments