Communities for your favorite technologies. Explore all Collectives
Stack Overflow for Teams is now called Stack Internal. Bring the best of human thought and AI automation together at your work.
Bring the best of human thought and AI automation together at your work. Learn more
Find centralized, trusted content and collaborate around the technologies you use most.
Stack Internal
Knowledge at work
Bring the best of human thought and AI automation together at your work.
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}"
f"{int('4,224'.replace(',','')):,.2f}"
Add a comment
Required, but never shown
By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.