Python 将数字四舍五入到最接近的十

Python 将数字四舍五入到最接近的十,python,rounding,Python,Rounding,我需要在我的程序中有一段代码,它将在我的代码中得到一个数字dSum,并将其四舍五入到最接近的10。例如,如果dSum为33,则需要将dSum四舍五入为40。我已有的那行代码是错误的,它似乎要四舍五入到最接近的10。我从另一个stackoverflow问题中找到了这段代码,但它在这种情况下不起作用 import math #Imports the math library def findCheckDigit(): code = input("Please enter your 7 di

我需要在我的程序中有一段代码,它将在我的代码中得到一个数字dSum,并将其四舍五入到最接近的10。例如,如果dSum为33,则需要将dSum四舍五入为40。我已有的那行代码是错误的,它似乎要四舍五入到最接近的10。我从另一个stackoverflow问题中找到了这段代码,但它在这种情况下不起作用

import math #Imports the math library

def findCheckDigit():
    code = input("Please enter your 7 digit code: ")  #Makes a function and sets the code to a variable

    d1 = int(code[0]) * 3
    d2 = int(code[1]) * 1
    d3 = int(code[2]) * 3
    d4 = int(code[3]) * 1    #Seperates each digit and assigns it to a variable, multiplying it by 3 or 1 
    d5 = int(code[4]) * 3
    d6 = int(code[5]) * 1
    d7 = int(code[6]) * 3

    dSum = d1 + d2 + d3 + d4 + d5 + d6 + d7      #Adds each digit together
    dSumRounded = int(math.ceil(dSum / 10.0)) * 10   #Gets the rounded up version of the sum of the digits
    checkDigit = dSumRounded - dSum   #Makes the check digit equal to the sum of digits taken away from the rounded up sum of digits

    print(dSumRounded)
    print("Your check digit is: " + str(checkDigit))   #Prints the check digit

我更喜欢呆在爱尔兰

>>> for i in range(20, 40):
        print(i, '->', (i + 9) // 10 * 10)

20 -> 20
21 -> 30
22 -> 30
23 -> 30
24 -> 30
25 -> 30
26 -> 30
27 -> 30
28 -> 30
29 -> 30
30 -> 30
31 -> 40
32 -> 40
33 -> 40
34 -> 40
35 -> 40
36 -> 40
37 -> 40
38 -> 40
39 -> 40

也就是说,你自己的表达式intmath.ceildSum/10.0*10也能用。

我试过了,它能用33四舍五入到40。所有数字都要四舍五入,31也要到40吗?好的,我注意到了这个问题。我尝试过其他7位代码,但由于某种原因返回0的是222222。似乎在某个地方出现了问题,但我被难住了。我设置dsum=22222222,并看到intmath.ceildsum/10.0*10会产生222230,正如人们所期望的那样。这对整数类型有效,但对浮点类型会遇到障碍。对。他们的数字是整数,只要说“;-早期的Java实现使用常量进行舍入,结果是灾难性的。