如何在 Python 中四舍五入后保留尾随零
How to keep trailing zeroes after rounding in Python
如何在 Single 行代码中四舍五入后得到带尾随零的输出。
My code:-
print (df[['smoker','age']].corr().round(4).iloc[0]['age'])
Current output :- -0.025
Expected output: -0.0250
您可以使用随 f 字符串功能提供的格式选项:
print(f"{df[['smoker','age']].corr().round(4).iloc[0]['age']:.4f}")
print("{0:.4f}".format(df[['smoker','age']].corr().round(4).iloc[0]['age']))
{0:.4f}
中的0指格式调用中的第一项。例如
"{0} - {1}".format(x, y)
将生成字符串 "x y"
但具有 x 和 y 的值。
.4f
是指使用4位浮点数精度
如何在 Single 行代码中四舍五入后得到带尾随零的输出。
My code:-
print (df[['smoker','age']].corr().round(4).iloc[0]['age'])
Current output :- -0.025
Expected output: -0.0250
您可以使用随 f 字符串功能提供的格式选项:
print(f"{df[['smoker','age']].corr().round(4).iloc[0]['age']:.4f}")
print("{0:.4f}".format(df[['smoker','age']].corr().round(4).iloc[0]['age']))
{0:.4f}
中的0指格式调用中的第一项。例如
"{0} - {1}".format(x, y)
将生成字符串 "x y"
但具有 x 和 y 的值。
.4f
是指使用4位浮点数精度