FluffyFlounder
FluffyFlounder

Reputation: 138

How to round to tens (python3)

I'm trying to round a number to the nearest ten (not tenth). e.g. 52 would go to 50, 29 would go to 30, 325 would go to 330, etc. How would I do this??

Upvotes: 0

Views: 257

Answers (3)

ThunderHorn
ThunderHorn

Reputation: 2035

Explanation: there is no correct way to solve a problem

Newbie variant

we take the number i.e 123

step one get the last digit.

in python we can do this if we convert it into a string and then take [-1] of it ie

int(str(123)[-1]) #will return 3

and then we implement the logic if it is greater than 5 or lesser than 5

def round_to_10(i):
    last_digit = int(str(i)[-1])  # it will give the last digit i.e 123 will return 3
    if last_digit >= 5:  # if 3 >= 5 sets round_up to True
        return i + (10 - last_digit)  # we add 10 to the number end subtract the extra

    return i - last_digit  # if the first condition never occurs we subtract the extra

Another approach

We can get the remaining value with % i.e we get if there is some values left if we take all 10ns out of the number. We can do it with the % operator

10%100 # returns 0 because there is no remaining value

10%123 # returns 3 and so on

This solution will work for negative numbers too.

def round_to_10(i):
    last_digit = i%10 
    if last_digit >= 5:
        return i + (10-last_digit)
    return i-last_digit
    In [7]: round_to_10(4)
    Out[7]: 0
    
    In [8]: round_to_10(5)
    Out[8]: 10
    
    In [9]: round_to_10(123)
    Out[9]: 120

Upvotes: 3

tominekan
tominekan

Reputation: 313

Try this: What it does is that it gets the turns the number to a string and gets the last character (to find the last digit), then it checks if the lastDigit is less than 5. If it is less than 5, then it subtracts the lastDigit from the number, eg 24 -> 20. Else, it adds (10 - lastDigit) to make it equal to the nearest 10th, eg 25 -> 30

def roundToTenth(num):
    num = round(num) # To get rid of small bugs
    # To get the last digit, we turn it to a string and take the last character
    lastDigit = int(str(num)[(len(str(num))-1)])
    diff = 10 - lastDigit
    if lastDigit < 5:
        return num - lastDigit
    else:
        return num + (10-lastDigit)

Upvotes: 0

hrokr
hrokr

Reputation: 3559

The simplest way is to use round(). Normally people think of this function as applying only to numbers past the decimal but using a negative number will accomplish what you're intending to do without recreating the wheel.

x = round(452.76543, -1)

>>> 450.0

If that decimal bothers you, prepend the round with an int statement int(round(452.76543, -1))

Now, I know you've already accepted the answer but consider what would happen if you had a decimal number (say 512.273). Using ThunderHorn's round_to_10 code you would get:

round_to_10(512.273)

>>> 509.273

Which doesn't work. It should be 510, which it is but only if you don't have a decimal value as in input.

But by using the built in function, you not only have less code, but robust and well-tested code that works in either case.

Upvotes: 5

Related Questions