python function to set accuracy of float

≡放荡痞女 提交于 2019-12-11 04:07:23

问题


I would like to make a function:

def accuracy(number, index):

For example

  • accuracy(2.5e-10, -5) would return 0.
  • accuracy(49, 2) would return 0.
  • accuracy(50, 2) would return 100.

    So basically it would round to the closest 10 power of the index index How would you do that?


回答1:


def accuracy(n, i):
    return round(float(n) / 10**i) * 10**i


来源:https://stackoverflow.com/questions/18219400/python-function-to-set-accuracy-of-float

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!