保留 3 位小数的浮点数除法

问题描述 投票:0回答:1

所以我需要做一些划分。有谁知道如何输出保留三位小数的结果?例如:--> 5/2 = 2.500 8/6 = 1.333 10/3 = 3.333

尝试过的代码:

a = int(input())
b = int(input())
print(a/b)

你可以看到,如果输入是5和2,它将输出2.5(不是小数点后3位)

python math division
1个回答
0
投票
print(f'{a/b:.3f}')

格式化迷你语言手册:https://docs.python.org/3/library/string.html#formatspec

© www.soinside.com 2019 - 2024. All rights reserved.