Monday, April 20, 2015

How to add a thousands comma separator to int and float types

Thousands

Integer Types

In [1]:
big_integer = 1234325231
In [2]:
type(big_integer)
Out[2]:
int
In [3]:
'{0:,d}'.format(big_integer)
Out[3]:
'1,234,325,231'

Float Types

In [4]:
big_float = 1234325231.00
In [5]:
type(big_float)
Out[5]:
float
In [7]:
'{0:,f}'.format(big_float)
Out[7]:
'1,234,325,231.000000'
In [8]:
'{0:,.2f}'.format(big_float) # .2 means round two decimals
Out[8]:
'1,234,325,231.00'
In [9]:
'{0:,.0f}'.format(big_float)# .0 means round to zero decimals
Out[9]:
'1,234,325,231'

1 comment: