Categories
Python Answers

How to do currency formatting in Python?

Spread the love

Sometimes, we want to do currency formatting in Python.

In this article, we’ll look at how to do currency formatting in Python.

How to do currency formatting in Python?

To do currency formatting in Python, we can use the locale module.

For instance, we write

import locale

locale.setlocale(locale.LC_ALL, '')
s = locale.currency(5676766.56, grouping=True)

to call setlocale to set the locale.

Then we call locale.currency with the currency value and the grouping argument to return a string with the number formatted as a currency.

We set grouping to True so that the digits are grouped with thousand separators.

Conclusion

To do currency formatting in Python, we can use the locale module.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *