Rezha Julio

The Hard Coded Chemist

Get the Most of Floats

2018-07-22
Similar to the int data type, floats also have several additional methods useful in various scenarios. For example, you can directly check if the float number is actually an integer with is_integer(): >>> (5.9).is_integer() False >>> (-9.0).is_integer() True Integer values might be preferred over floats in some cases and you can convert a float to a tuple matching a fraction with integer values: >>> (-5.5).as_integer_ratio() (-11,2) # -11 / 2 == -5. Continue reading