在Python中,保留小数点后四位可以通过以下几种方法实现:
1. 使用 `format` 函数:
a = 1.
formatted_a = format(a, '.4f')
print(formatted_a) 输出:1.2346
2. 使用 `round` 函数:
a = 1.
rounded_a = round(a, 4)
print(rounded_a) 输出:1.2346
a = 1.
formatted_a = '%.4f' % a
print(formatted_a) 输出:1.2346
以上方法都会进行四舍五入到指定的小数位数。选择哪种方法取决于你的个人喜好和具体的使用场景