【问题标题】:Python 3 code - Rounding integers [duplicate]Python 3代码 - 舍入整数[重复]
【发布时间】:2017-01-04 22:37:04
【问题描述】:

我正在尝试在 python 3 中向上舍入数字。在我现有的代码中,数字向上舍入到最接近的 10 或向下舍入。例如,67 到 70,64 到 60。我希望这个数字总是四舍五入到最接近的 10 的倍数,这样67-->7064-->70。这是我到目前为止的四舍五入代码:

##ROUNDING SumOfUsrinput TO NEAREST 10##
SumOfUsrinput=int(input("Please enter the sum: "))
SumRounded=round(SumOfUsrinput,-1)
print (SumRounded)

如果你能简单地回答并解释它是如何工作的,我将不胜感激。

【问题讨论】:

    标签: python python-3.x integer rounding


    【解决方案1】:

    向上舍入的一种方法是使用整数除法降低到您想要的精度,然后再向上乘。例如:

    Sumrounded = SumOfusrinput // (-10) * (-10)
    

    【讨论】:

      猜你喜欢
      • 2016-01-23
      • 1970-01-01
      • 2013-04-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-02-07
      • 2018-05-11
      相关资源
      最近更新 更多