Anonymous user
Price fraction: Difference between revisions
m
→{{header|Python}}: (Moved constants out of function body)
(added Ursala) |
m (→{{header|Python}}: (Moved constants out of function body)) |
||
Line 378:
<lang python>>>> import bisect
>>> def pricerounder(pricein):
▲ cin = [0.06, 0.11, 0.16, 0.21, 0.26, 0.31, 0.36, 0.41, 0.46, 0.51, 0.56, 0.61, 0.66, 0.71, 0.76, 0.81, 0.86, 0.91, 0.96, 1.01]
▲ cout = [0.10, 0.18, 0.26, 0.32, 0.38, 0.44, 0.50, 0.54, 0.58, 0.62, 0.66, 0.70, 0.74, 0.78, 0.82, 0.86, 0.90, 0.94, 0.98, 1.00]
▲ return cout[ bisect.bisect_right(cin, pricein) ]</lang>
When dealing with money it is good to think about possible loss of precision. If we change the units to be integer cents we could use the following exact routine:
|