开发者

did you know a method to show this value using python

i know floor in python :

floor(0.2) #---> 0

but i want to get :

 1 2 3 4 5 6 7 8 9 10 #---> 1
 11 12开发者_运维百科 13 ...  20 # --->2
 21 22 23 ... 30 #--->3

has a method to get this value ,

thanks


>>> (21 + 9) // 10
3
>>> (30 + 9) // 10
3


The easiest solution (without the need to import math module) is:

(x-1) // 10 + 1

which will make sure you get an integer (thanks to //). But if you insist on using floor(), then here you go:

import math
math.floor ( (x - 1) / 10. ) + 1

For example:

  • x = 1: floor ( (1 - 1) / 10. ) + 1 = floor (0) + 1 = 1
  • x = 9: floor ( (9 - 1) / 10. ) + 1 = floor (8 / 10) + 1 = 1
  • x = 10: floor ( (10 - 1) / 10. ) + 1 = floor (9 / 10) + 1 = 1
  • x = 11: floor ( (11 - 1) / 10. ) + 1 = floor (10 / 10) + 1 = 2
  • and so on...

EDIT:

I have updated my answer and got rid of division importing (which simplifies the solution), following the advice of martineau. Thanks!

EDIT2:

Updated my answer with (x-1) // 10 + 1) solution, which does not need additional modules and looks like the faster one.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜