简体   繁体   中英

Python numeric format

What is a good way to format a python decimal like this way?

115 --> 115.00

And

4,224 --> 4,224.00

If you're using python 3.6+, a good option is f-strings:

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}"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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