简体   繁体   English

Python数字格式

[英]Python numeric format

What is a good way to format a python decimal like this way? 像这样格式化python十进制的好方法是什么?

115 --> 115.00 115-> 115.00

And

4,224 --> 4,224.00 4,224-> 4,224.00

If you're using python 3.6+, a good option is f-strings: 如果您使用的是Python 3.6+,则f字符串是一个不错的选择:

f'{115:,.2f}'
f'{4224:,.2f}'

If the 4224 is actually a string (maybe because of the comma), you will need to transform it to a number before f"{int('4,224'.replace(',','')):,.2f}" 如果4224实际上是字符串(可能是因为逗号),则需要将其转换为f"{int('4,224'.replace(',','')):,.2f}"

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

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