1

I would like to round values to the nearest 10 (0-4 = 0; 5-14 = 1; 15-24=2, etc.) I can get round to nearest 10, but I want to single digits for each range of numbers, as the example shows.

From -5 to 4 = 0, 5 to 14 = 1, 15 to 24 = 2 etc...

What I have tried...

int(math.ceil(x / 10.0)) * 10

This gives to nearest 10, but starts from 0 and gives to nearest 10 instead of single digit.

Any advice is helpful.

9
  • 95 - 104, what should the rusult be? Single digit again? Commented Oct 25, 2017 at 13:59
  • no, that can be double digits. Commented Oct 25, 2017 at 14:01
  • 1
    This can help: docs.python.org/2/library/functions.html#round Commented Oct 25, 2017 at 14:03
  • 3
    (x + 5) // 10 ? This assumes x is an integer. Commented Oct 25, 2017 at 14:04
  • 1
    I does for me on python 2.7 and 3.5, so I'm curious exactly what you're getting? Commented Oct 25, 2017 at 14:15

1 Answer 1

1

We can simply use:

int(round(float(x)/10))

From the documentation:

round(number[, ndigits])

Return the floating point value number rounded to ndigits digits after the decimal point. If ndigits is omitted, it defaults to zero. The result is a floating point number. Values are rounded to the closest multiple of 10 to the power minus ndigits; if two multiples are equally close, rounding is done away from 0 (so, for example, round(0.5) is 1.0 and round(-0.5) is -1.0).

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, I got it to work like this int(round(x, -1 ) / 10)

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.