【问题标题】:How to round float to certain amount of decimal places Python [duplicate]如何将浮点数舍入到一定数量的小数位Python [重复]
【发布时间】:2021-07-28 17:22:09
【问题描述】:

我有一个从随机库中随机生成的浮点数,我想根据其他数字将其缩短到任意数量的位置。

例子:

输入 = 4.06098152

处理数字,四舍五入到千位

输出 = 4.061

我该怎么做?

【问题讨论】:

  • print(round(float(input),3))?
  • 意识到即使是完美的四舍五入,一些十进制数也不能完全表示为real:Is floating point math broken?
  • 我很好奇你在搜索“python rounding”时发现了什么。
  • @MarkRansom 不多。

标签: python math rounding


【解决方案1】:

您可以使用内置的round 函数: 括号中的第二个参数给出了要四舍五入的位数

x=input("Enter a decimal number: ")
print(round(float(x),3))

【讨论】:

    猜你喜欢
    • 2012-11-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多