Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python locale currency to 0 decimals

I can't figure out how to set my currency to 0 decimals. for now it always puts .00 behind my currency.

locale.setlocale(locale.LC_ALL, 'en_US.UTF-8')
damn = locale.currency(self.damn, grouping=True).replace('$','') + " Dmn"

self.damn is always an integer.

like image 1000
Hans de Jong Avatar asked Oct 29 '25 16:10

Hans de Jong


2 Answers

It seems like you are just interested in grouping. You don't need to use the currency function for this. Use locale.format():

import locale
locale.setlocale(locale.LC_ALL, 'en_US.UTF-8')
damn = '{0} Dmn'.format(locale.format('%d', self.damn, True))

And if you don't depend on the locale stuff, you can group the number with string.format(), too:

# Comma as separator
damn = '{:,} Dmn'.format(self.damn)
# Locale aware separator
damn = '{:n} Dmn'.format(self.damn)
like image 193
WolleTD Avatar answered Oct 31 '25 07:10

WolleTD


The output is a string so add [:-3] to the end it:

a = locale.currency(num, grouping=True)[:-3]
like image 32
Jaysin Koregaokar Avatar answered Oct 31 '25 06:10

Jaysin Koregaokar