Add a decimal point with number of digits .2f see the docs: https://docs.python.org/2/library/string.html#format-specification-mini-language :
In [212]:
"{0:,.2f}".format(2083525.34561)
Out[212]:
'2,083,525.35'
For python 3 you can use f-strings (thanks to @Alex F):
In [2]:
value = 2083525.34561
f"{value:,.2f}"
Out[2]:
'2,083,525.35'
Answer from EdChum on Stack OverflowAdd a decimal point with number of digits .2f see the docs: https://docs.python.org/2/library/string.html#format-specification-mini-language :
In [212]:
"{0:,.2f}".format(2083525.34561)
Out[212]:
'2,083,525.35'
For python 3 you can use f-strings (thanks to @Alex F):
In [2]:
value = 2083525.34561
f"{value:,.2f}"
Out[2]:
'2,083,525.35'
("%.2f" % 2083525.34561).replace(".", ",")
How to format a number with comma and 2 decimal?
How to format a number with comma and specified precision digits in Python - Stack Overflow
python - How to print a number using commas as thousands separators - Stack Overflow
How do I put commas between numbers?
I am trying to format a number like this 250,000.50
What I have tried
<td style="text-align:right;">${{"{0:,.2f}"|format(data[0]['amount']) }} </td>
<td style="text-align:right;">${{"{:0.2f}"|format(data[0]['amount']) }} </td>
<td style="text-align:right;">${{"{0:%.2f}"|format(data[0]['amount']) }} </td>
data[0]['amount']contains 250000.505050
In Python 2.7 and 3.x you can use str.format for this:
>>> num = 1234567890.0876543
>>> "{0:,f}".format(num)
'1,234,567,890.087654'
>>> "{0:,.2f}".format(num)
'1,234,567,890.08'
>>> "{0:,f}".format(1234)
'1,234.000000'
I don't think you looked deep enough into the locale module. locale.format() is what you want, but make sure you set a locale first or you won't get grouping at all.
>>> locale.setlocale(locale.LC_ALL, '')
'en_US.UTF-8'
>>> locale.format("%.4f", 12345.678912, grouping=True)
'12,345.6789'
Locale-agnostic: use _ as the thousand separator
f'{value:_}' # For Python ≥3.6
Note that this will NOT format in the user's current locale and will always use _ as the thousand separator, so for example:
1234567 ⟶ 1_234_567
English style: use , as the thousand separator
'{:,}'.format(value) # For Python ≥2.7
f'{value:,}' # For Python ≥3.6
Locale-aware
import locale
locale.setlocale(locale.LC_ALL, '') # Use '' for auto, or force e.g. to 'en_US.UTF-8'
'{:n}'.format(value) # For Python ≥2.7
f'{value:n}' # For Python ≥3.6
Reference
Per Format Specification Mini-Language,
The
','option signals the use of a comma for a thousands separator. For a locale aware separator, use the'n'integer presentation type instead.
and:
The
'_'option signals the use of an underscore for a thousands separator for floating point presentation types and for integer presentation type'd'. For integer presentation types'b','o','x', and'X', underscores will be inserted every 4 digits.
I'm surprised that no one has mentioned that you can do this with f-strings in Python 3.6+ as easy as this:
>>> num = 10000000
>>> print(f"{num:,}")
10,000,000
... where the part after the colon is the format specifier. The comma is the separator character you want, so f"{num:_}" uses underscores instead of a comma. Only "," and "_" is possible to use with this method.
This is equivalent of using format(num, ",") for older versions of python 3.
This might look like magic when you see it the first time, but it's not. It's just part of the language, and something that's commonly needed enough to have a shortcut available. To read more about it, have a look at the group subcomponent.
Let's say I have 2792819, now I want it to be like 2,792,819. How do I do it?
I can do so while reversing it and after every 3 iterations, put a comma or something like that. But is there a better way to do so?
In Python 2.7 and 3.x, you can use the format syntax :,
>>> total_amount = 10000
>>> print("{:,}".format(total_amount))
10,000
>>> print("Total cost is: ${:,.2f}".format(total_amount))
Total cost is: $10,000.00
This is documented in PEP 378 -- Format Specifier for Thousands Separator and has an example in the Official Docs "Using the comma as a thousands separator"
if you are using Python 3 or above, here is an easier way to insert a comma:
First way
value = -12345672
print (format (value, ',d'))
or another way
value = -12345672
print ('{:,}'.format(value))