Round float to x decimals?

前端 未结 4 1104
一向
一向 2020-11-28 05:19

Is there a way to round a python float to x decimals? For example:

>>> x = roundfloat(66.66666666666, 4)
66.6667
>>>x = roundfloat(1.295782         


        
4条回答
  •  刺人心
    刺人心 (楼主)
    2020-11-28 05:30

    Use the built-in function round():

    In [23]: round(66.66666666666,4)
    Out[23]: 66.6667
    
    In [24]: round(1.29578293,6)
    Out[24]: 1.295783
    

    help on round():

    round(number[, ndigits]) -> floating point number

    Round a number to a given precision in decimal digits (default 0 digits). This always returns a floating point number. Precision may be negative.

提交回复
热议问题