80

First of all, I have tried this post (among others): Currency formatting in Python. It has no affect on my variable. My best guess is that it is because I am using Python 3 and that was code for Python 2. (Unless I overlooked something, because I am new to Python).

I want to convert a float, such as 1234.5, to a String, such as "$1,234.50". How would I go about doing this?

And just in case, here is my code which compiled, but did not affect my variable:

money = float(1234.5)
locale.setlocale(locale.LC_ALL, '')
locale.currency(money, grouping=True)

Also unsuccessful:

money = float(1234.5)
print(money) #output is 1234.5
'${:,.2f}'.format(money)
print(money) #output is 1234.5
7
  • The latter option works in both Python 2.7 and 3.3. Jan 18, 2014 at 19:04
  • does not appear to work, as discussed in your answer
    – Evorlor
    Jan 18, 2014 at 19:05
  • Something else is going on with your code. Can you post more of the context? Jan 18, 2014 at 19:07
  • k i posted updated version. any ideas?
    – Evorlor
    Jan 18, 2014 at 19:09
  • 5
    Ah, you need to assign money (or a new variable) to '${:,.2f}'.format(money). For example, try money = '${:,.2f}'.format(money), and then print out money. Jan 18, 2014 at 19:10

6 Answers 6

175

In Python 3.x and 2.7, you can simply do this:

>>> '${:,.2f}'.format(1234.5)
'$1,234.50'

The :, adds a comma as a thousands separator, and the .2f limits the string to two decimal places (or adds enough zeroes to get to 2 decimal places, as the case may be) at the end.

6
  • '${:,.2f}'.format(money) after money = float(1234.5) has no affect. Did I make a mistake?
    – Evorlor
    Jan 18, 2014 at 19:01
  • 1
    @Evorlor Yes, that works for me in both Python 3.3 and 2.7. Did you assign money to a variable that you need to print out? Jan 18, 2014 at 19:03
  • yes i did and have confirmed that by printing money before and after
    – Evorlor
    Jan 18, 2014 at 19:04
  • 2
    This doesn't work for negative values. '${:,.2f}'.format(-2) returns '$-2.00'. locale.currency(-2, grouping=True) returns '-$2.00'. Just import locale and call locale.setlocale(locale.LC_ALL, '') Jan 12, 2016 at 14:18
  • 1
    locale works, but this also works in python3: ` (money<0)*'-' + '${:,.2f}'.format(money)`.
    – jyalim
    Jul 22, 2016 at 6:50
19

In python 3, you can use:

import locale
locale.setlocale( locale.LC_ALL, 'English_United States.1252' )
locale.currency( 1234.50, grouping = True )

Output

'$1,234.50'
2
17

Building on @JustinBarber's example and noting @eric.frederich's comment, if you want to format negative values like -$1,000.00 rather than $-1,000.00 and don't want to use locale:

def as_currency(amount):
    if amount >= 0:
        return '${:,.2f}'.format(amount)
    else:
        return '-${:,.2f}'.format(-amount)
1
  • 9
    Nice... which got me thinking about doing this in a pinch without a def... boolean slicing... "{}${:,.2f}".format(["","-"][amount<0], abs(amount))
    – NaN
    Sep 21, 2016 at 8:26
4

Personally, I like this much better (which, granted, is just a different way of writing the currently selected "best answer"):

money = float(1234.5)
print('$' + format(money, ',.2f'))

Or, if you REALLY don't like "adding" multiple strings to combine them, you could do this instead:

money = float(1234.5)
print('${0}'.format(format(money, ',.2f')))

I just think both of these styles are a bit easier to read. :-)

(of course, you can still incorporate an If-Else to handle negative values as Eric suggests too)

-2

you said that:

`mony = float(1234.5)
print(money)      #output is 1234.5
'${:,.2f}'.format(money)
print(money)

did not work.... Have you coded exactly that way? This should work (see the little difference):

money = float(1234.5)      #next you used format without printing, nor affecting value of "money"
amountAsFormattedString = '${:,.2f}'.format(money)
print( amountAsFormattedString )
-2
df_buy['BUY'] = df_buy['BUY'].astype('float')
df_buy['BUY'] = ['€ {:,.2f}'.format(i) for i in list(df_buy['BUY'])]

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.