How to format a float with space between thousands, millions, etc

Question:

With :

pd.options.display.float_format = '{:,}'.format

It displays for number x = 1234567.888 :

1,234,567.888

What is the appropriate format to show 0 decimals and add a space between thousands, millions, billions, and so on ? Like this

1 234 568
Asked By: Vincent

||

Answers:

To perform this operation you are looking for using python, the following does the trick:

from math import trunc
some_float = 1234569.02

print ('{:,}'.format(trunc(some_float)).replace(',', ' '))

You can read more about trunc() here.

You can also use ‘{:,.0f}’ to format, as pointed out by @Jon Clements. That does not require importing the math library.

Answered By: pragmaticprog
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.