Rounding Rules in Python -


i trying round different rules within python. example;

10.666 = 10.6 10.667 = 10.7 

ie down on 6, , on 7.

is there way can python?

i'm not sure sort of rounding rules have in mind. can give more detail on rounding rules?

therefore can't exactly right, suspect use pattern implementation.

def cround(v):     """     round number down @ 1st decimal place when digit in      3rd decimal place <= 6, when >= 7     """     v *= 10     q = str(round(v, 2))     if int(q[-1]) <= 6:         return int(v) / 10.0     return round(v) / 10.0  nums = [     10.666, 10.667, 0.1, 1.0, 10.11, 10.22, 10.06, 10.006, 11.6, 11.7,     10.666123, 10.667123, 10.888, 10.999 ]  num in nums:     print str(num).ljust(11), cround(num) 

output:

10.666      10.6 10.667      10.7 0.1         0.1 1.0         1.0 10.11       10.1 10.22       10.2 10.06       10.0 10.006      10.0 11.6        11.6 11.7        11.7 10.666123   10.6 10.667123   10.7 10.888      10.9 10.999      11.0 

Comments