Python 3.x rounding behavior

前端 未结 11 1687
别那么骄傲
别那么骄傲 2020-11-22 00:41

I was just re-reading What’s New In Python 3.0 and it states:

The round() function rounding strategy and return type have changed. Exact halfway cas

11条回答
  •  不思量自难忘°
    2020-11-22 01:08

    Try this code:

    def roundup(input):   
       demo = input  if str(input)[-1] != "5" else str(input).replace("5","6")
       place = len(demo.split(".")[1])-1
       return(round(float(demo),place))
    

    The result will be:

    >>> x = roundup(2.5)
    >>> x
    3.0  
    >>> x = roundup(2.05)
    >>> x
    2.1 
    >>> x = roundup(2.005)
    >>> x
    2.01 
    

    Ooutput you can check here: https://i.stack.imgur.com/QQUkS.png

提交回复
热议问题