【问题标题】:Limit Output to only two decimal places将输出限制为小数点后两位
【发布时间】:2022-01-11 09:53:39
【问题描述】:

输出:

1.0克水中的水分子数为:3.341842397336293e+22

预期输出:

1.0克水中的水分子数为:3.342e+22

尝试使用 round()

p = round(n,3)
n = 3.341842397336293e+22
p = round(n,3)
print(p)

输出:

3.341842397336293e+22

尝试过

  n = 3.341842397336293e+22
    print("%.3f"%n)

输出:

33418423973362928713728.000

【问题讨论】:

    标签: python decimal rounding


    【解决方案1】:

    您可以使用.3e 作为format

    n = 3.341842397336293e+22
    print(f"... water is: {n:.3e}") # ... water is: 3.342e+22
    

    'e':科学记数法。对于给定的精度p,以科学计数法格式化数字,用字母“e”分隔系数和指数。系数小数点前有一位,小数点后有p位,共有p + 1位有效位。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-04-01
      • 1970-01-01
      • 2023-01-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-03
      相关资源
      最近更新 更多