Search code examples
pythonnumbersformat

Python number with comma as 1000 separator


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))))

?


Solution

  • 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.