2

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
  • ... by using round()? What is the problem with your code, do you get an error or incorrect results? Commented Aug 13, 2013 at 21:20

1 Answer 1

3
def accuracy(n, i):
    return round(float(n) / 10**i) * 10**i
Sign up to request clarification or add additional context in comments.

1 Comment

round(float(n), -i) is simpler.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.