June 2019
Beginner to intermediate
770 pages
19h 24m
English
We truncated the presentation on rounding. We defined the required functions for round() and trunc() without further explanation. These definitions are the minimum requirements of the abstract superclass. However, these definitions are not quite enough for our purposes.
To process currency, we'll often have code that looks like this:
>>> price = FixedPoint(1299, 100) >>> tax_rate = FixedPoint(725, 1000) >>> price * tax_rate FixedPoint(941775, scale=100000)
Then, we need to round this value to a scale of 100 to get a value of 942. We need methods that will round (as well as truncate) a number to a new scale factor. The following is a method to round to a specific scale:
def round_to(self, new_scale: int) -> ...
Read now
Unlock full access