2

I have a list of numbers that are currency values that I would like to convert to currencies with symbol:

Current list

list = [200,4002,4555,7533]

Given below is what is returned if I print the above list:

[(Decimal('200'), Decimal('4002'), Decimal('4555'), Decimal('7533')]

Expected output

list = ['$200','$4,002','$4,555','$7,533'] <<--These are basically number formatted for 1000's separator with the currency symbol
scott martin
  • 1,253
  • 1
  • 14
  • 36

3 Answers3

2

Assuming you want the output as strings:

In [123]: lst = [200,4002,4555,7533]

In [124]: [f'${cur:,}' for cur in lst]
Out[124]: ['$200', '$4,002', '$4,555', '$7,533']

Also don't name your variables as some built-in.

heemayl
  • 39,294
  • 7
  • 70
  • 76
  • thanks for the reply.. I am getting an error `TypeError: unsupported format string passed to tuple.__format__` when I try in my data.. Any thoughts as to what is causing that.. – scott martin Oct 24 '19 at 09:12
  • @scottmartin Which version of Python are you on? – heemayl Oct 24 '19 at 10:18
  • using python 3.7 – scott martin Oct 24 '19 at 10:25
  • @scottmartin That's strange. How did you run it? Can you run it as I did in a fresh session? – heemayl Oct 24 '19 at 10:27
  • Yes, I have tried running this in different environments, get the same result.. However if I use the sample list you used it works fine so I am not sure if it has to do with my data. But they are just numbers that I am trying to convert.. – scott martin Oct 24 '19 at 10:29
  • @scottmartin Then it's that. The logic here is pretty straight-forward. – heemayl Oct 24 '19 at 10:30
0

Avoid using builtin names like list for variables. You also can't output in this format without converting to strings, like below:

l = [200, 4002, 4555, 7533]

l = ['${:0,.0f}'.format(x) for x in l]
print(l)

Prints:

['$200', '$4,002', '$4,555', '$7,533']
0

Instead of manually formatting the values in your list to strings containing currency representations, it's a good idea to use standard library functions that are dedicated to exactly this job. Here, what you may want to use are the functions from the locale module. These functions provide a convenient way for instance to represent dates, times, and currencies in a format that is appropriate for the current locale settings. The locale can be either set by the operating system of the computer the program runs on, or by the programmer of the program itself.

The first thing you have to do is load the locale module. Then you set up the locale setting, either to the system default:

import locale

locale.setlocale(locale.LC_ALL, "")   # to use the system-wide locale setting

or to a locale of your own choosing:

import locale

locale.setlocale(locale.LC_ALL, "en_US.utf-8")   # to use the USA locale

Now, you can use the currency function to format a value using the current locale setting:

lst = [200, 4002, 4555, 7533]
cur_lst = [locale.currency(val) for val in lst]

print(cur_list)
['$200.00', '$4002.00', '$4555.00', '$7533.00']

The currency function has three options that allow you to tweak the output. The grouping option will insert appropriate grouping marks to separate thousands, millions etc.:

locale.currency(123456, grouping=True)
'$123,456.00'

locale.currency(123456, grouping=False)  # the default
'$123456.00'

The international option uses the international standard currency abbreviation instead of the currency symbol:

locale.currency(123456, international=True)
'USD 123456.00'

locale.currency(123456, international=False)  # the default
'$123456.00'

Lastly, setting the symbol option to True suppresses showing the currency symbol altogether.

Schmuddi
  • 1,995
  • 21
  • 35