小数点后带有f字符串的固定数字 [英] Fixed digits after decimal with f-strings
本文介绍了小数点后带有f字符串的固定数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
Python f字符串是否有一种简单的方法来固定小数点后的位数? (特别是f字符串,不是其他字符串格式设置选项,例如.format或%)
Is there an easy way with Python f-strings to fix the number of digits after the decimal point? (Specifically f-strings, not other string formatting options like .format or %)
例如,假设我要在小数点后两位显示数字.
For example, let's say I want to display 2 digits after the decimal place.
我该怎么做?这么说
a = 10.1234
推荐答案
在格式表达式中包括类型说明符:
Include the type specifier in your format expression:
>>> a = 10.1234
>>> f'{a:.2f}'
'10.12'
这篇关于小数点后带有f字符串的固定数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文