开发者

Rounding Rules in Python

I am trying to round to some different rules within python. For example;

10.666 = 10.6
10.667 = 10.7

ie down on 6, and up on 7.

Is 开发者_高级运维there a way that I can do this with Python?


Use decimal.Decimal. It has a variety of sophisticated rounding rules built-in and debugged.

http://docs.python.org/library/decimal.html


I'm not sure exactly what sort of rounding rules you have in mind. Can you give more detail on your rounding rules?

Therefore I can't say this is exactly right, but I suspect you could use it as a pattern for your implementation.

def cround(v):
    """
    Round number down at 1st decimal place when the digit in the 
    3rd decimal place is <= 6, up 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 ]

for 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


If what you want is to round up if the part-to-be-rounded is larger than or equal to ⅔ of its maximum value, else round down, then I believe you can use the plain round() function by first subtracting ⅙ * wanted precision:

def my_round(n):
    return round(n - .1/6, 1)

>>> print(my_round(10.666))
10.6
>>> print(my_round(10.667))
10.7


Yes, you can. As you have not described to rules, but I am assume you are aware of what you want, you can covert the number to string, iterate it over character by character and once you reach the character 2 steps beyond decimal. you can act upon your rule.

There is also a builin round function, which can it round the number to a given precision.


Here is one way to do it (using simplebias's test data)

>>> def cround(v):
...     return round(v-2.0/9, 1)+.2
... 
>>> 
>>> 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 ]
>>> 
>>> for num in NUMS:
...     print str(num).ljust(11), cround(num)
... 
10.666      10.6
10.667      10.6
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.6
10.888      10.9
10.999      11.0


I wanna keep it as simple as possible.

You have three functions available ceil, floor, round. The rounding rules are as follows:

  • ceil() rounds up, which means:

    ceil(10.1) = 11, ceil(10.9) = 11

  • floor() rounds down, which means:

    floor(10.1) = 10, floor(10.9) = 10

  • round() rounds depending on your value (here, up (>=10.5) or down(<10.5)), which means:

    round(10.1) = 10, round(10.9) = 11

If you use ceil or floor you first have to import the math module, e.g. from math import ceil.

For more information, I suggest, you have a look here: Floating Point Arithmetic: Issues and Limitations

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜