简体   繁体   English

将 Output 限制为仅两位小数

[英]Limit Output to only two decimal places

Output: Output:

number of water molecules in 1.0 gm of water is: 3.341842397336293e+22 1.0克水中的水分子数为:3.341842397336293e+22

Expected Output:预期 Output:

number of water molecules in 1.0 gm of water is: 3.342e+22 1.0克水中的水分子数为:3.342e+22

Tried using round()尝试使用 round()

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

output: output:

3.341842397336293e+22 3.341842397336293e+22

and tried并尝试过

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

output: output:

33418423973362928713728.000 33418423973362928713728.000

You can use .3e as the format :您可以使用.3e作为格式

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

'e' : Scientific notation. 'e' :科学记数法。 For a given precision p , formats the number in scientific notation with the letter 'e' separating the coefficient from the exponent.对于给定的精度p ,以科学记数法格式化数字,用字母“e”将系数与指数分开。 The coefficient has one digit before and p digits after the decimal point, for a total of p + 1 significant digits.系数小数点前有一位,小数点后有p位,共p + 1位有效数字。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM