Convert float to comma-separated string
Question: How would I convert a float into its 'accounting form' --

    
    
    100028282.23 --> 100,028,282.23
    100028282 --> 100,028,282.00
    

Is there a python method that does this?


Answer: You can use the
[`locale.format()`](http://docs.python.org/library/locale.html#locale.format)
function to do this:

    
    
    >>> import locale
    >>> locale.setlocale(locale.LC_ALL, 'en_US.utf8')
    'en_US.utf8'
    >>> locale.format("%.2f", 100028282.23, grouping=True)
    '100,028,282.23'
    

Note that you have to give the precision: `%.2f`

Alternatively you can use the
[`locale.currency()`](http://docs.python.org/library/locale.html#locale.currency)
function, which follow the
[`LC_MONETARY`](http://docs.python.org/library/locale.html#locale.LC_MONETARY)
settings:

    
    
    >>> locale.currency(100028282.23)
    '$100028282.23'
    

