Python中是否有一个函数允许我向下舍入到整数的最接近的倍数?
round_down(19,10)=10
round_down(19,5)=15
round_down(10,10)=10
Run Code Online (Sandbox Code Playgroud)
我认真看了SO并没有发现任何有关四舍五入下来到最近的基地.在发布相关问题的链接或标记为重复之前,请记住这一点.
大家好,我目前正在做一个学校项目,甚至我的老师都很难过.在加拿大,便士已被删除,所以现在所有购买都被四舍五入为0或5.例如5.53将变为5.55,5.52将变为5.50.我试图让我的程序像这样循环,但我无法弄清楚如何.我知道如何舍入到小数位,但我不知道如何舍入到这样的细节.任何帮助,将不胜感激!
这是我的代码.该项目是关于制作出纳员将在咖啡店使用的程序.
order = ['coffee', 'tea', 'hashbrown','jelly','cream','chocolate','glazed','sandwich','bagel','cookie','pannini']
quantity = ['0','0','0','0','0','0','0','0','0','0','0']
# coffee = $1
# Tea = $1.30
# hashbrown = $1.25
# all donuts = $1.50
# sandwich = $2.50
# bagel = $2
# cookie = $0.50
# pannini = $4
cashier = 1
total = 0
while cashier == 1:
print "What did the customer order?"
ordered = input ()
while ordered > 10 or ordered < 0:
print "Do you want to input a valid order?" …Run Code Online (Sandbox Code Playgroud) 我有一个这个小任务,我将数字四舍五入到十的更高倍数。但是想要将其四舍五入到最接近的十的倍数。
我的代码:
import math
a = map(int,list(str(7990442)))
b = map(int,list(str(1313131)))
print "a :",a
print "b :",b
l= []
for p,q in zip(a,b):
l.append(p*q)
print "prod list :",l
ls = sum(l)
print "sum :",ls
def roundup(x):
return int(math.ceil(x / 10.0)) * 10
top = roundup(ls)
print "round value: ",top
val = top-ls
print "val :",val
a.append(val)
print "output :",a
Run Code Online (Sandbox Code Playgroud)
输出 :
a : [7, 9, 9, 0, 4, 4, 2]
b : [1, 3, 1, 3, 1, 3, 1] …Run Code Online (Sandbox Code Playgroud) 我得到了这个号码1.12412,我想round it to 1.12415或1.12410 (muliple of 5 in last decimal)
如果使用该Round(X,4)功能,我会得到1.1241 (4 decimals).
有没有一个功能可以做到这一点?谢谢!
堆栈中有一个答案,但使用的是 c# 而不是 python