将输出限制为仅小数点后两位

Limit Output to only two decimal places

输出:

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

您可以使用 .3e 作为 format:

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

'e': Scientific notation. For a given precision p, formats the number in scientific notation with the letter ‘e’ separating the coefficient from the exponent. The coefficient has one digit before and p digits after the decimal point, for a total of p + 1 significant digits.