Easy task:
number = 100123.44
number_formated = "100,123"
Don't tell me there is no better way than:
number_formated = "{:,}".format(int(format("{:.0f}".format(number))))
?
Easy task:
number = 100123.44
number_formated = "100,123"
Don't tell me there is no better way than:
number_formated = "{:,}".format(int(format("{:.0f}".format(number))))
?
To get the number with ,
as a thousands separator as a whole number, use this format string:
number_formated = "{:,.0f}".format(number)
No need to nest two calls and cast the resulting string to int again.
You can simply call int
on your number
without formatting:
number_formatted = "{:,}".format(int(number))
If you use Python 3.6 or higher you can use f-strings
:
number_formatted = f'{int(number):,}'
Edit: using Christian König answer we can remove the int
call:
number_formatted = f"{number:,.0f}"
You can use this
formatted_number = "{:,}".format(number)