繁体   English   中英

如何在 Python 中舍入后保持尾随零

[英]How to keep trailing zeroes after rounding in Python

单行代码中舍入后如何获得带有尾随零的输出。

My code:- 
   print (df[['smoker','age']].corr().round(4).iloc[0]['age'])

Current output :- -0.025

Expected output: -0.0250

您可以使用 f-string 功能提供的格式选项:

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:.4f}指的是格式调用中的第一项。 例如

"{0} - {1}".format(x, y)

将产生字符串"xy"但具有 x 和 y 的值。

.4f是指使用 4 位浮点精度

暂无
暂无

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

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