thesdog
thesdog

Reputation: 9

Truncate float to multiple of 10

I have the number 67.14, for example.

I need to set another variable as the next multiple of 10 down (60, in this case).

Would it be possible to just get the "7.14" from "67.14" and take it away?

Upvotes: 1

Views: 129

Answers (4)

user5547025
user5547025

Reputation:

Use // to get the floored quotient of x and y:

67.14 // 10 * 10

Result:

60.0

Use % to get the remainder of x / y:

67.14 % 10

Result:

7.140000000000001

Upvotes: 3

Bruce
Bruce

Reputation: 7132

There is an easier solution:

  • divide by 10
  • round to integer
  • multiply by 10

    >> int(11.7/10)*10
    10
    

Upvotes: 0

wizzkid
wizzkid

Reputation: 207

The % (modulo) sign should help you here:

new = old - (old % 10)

Upvotes: 0

mvelay
mvelay

Reputation: 1520

Type:

n = 67.14
print n - n % 10

>> result is 60.0

Upvotes: 0

Related Questions