我想要对两个浮点数进行取整,但是它输出的结果没有小数,仅仅将其四舍五入到最接近的整数,例如21.3会变为21。当我加上“,2”后,它应该四舍五入到最近的十分位。
新代码:
def add(op1,op2):
result = int(round(op1 + op2, -1))
print("")
print ("Output: %d + %d = %d" % (op1, op2, result))
输出:
Output: 10 + 10 = 20
新
输出:
Output: 10.3 + 10.9 = 21.0
代码:
def add(op1,op2):
result = int(round(op1 + op2, 1))
print("")
print ("Output: %0.1f + %0.1f = %0.1f" % (op1, op2, result))
when I put ",2"
" 是什么意思 - 你能展示一下你的代码吗? - Burhan Khalid"%.1f"
而不是"%d"
吗? - wflynnyprint("")
的目的是什么? - wflynnyint(-0.9) == 0
)。你确定这样做是正确的吗? - jpkotta