Skip to content Skip to sidebar Skip to footer

Using Dot As Thousand Separator In Python2.7

How to format a decimal number 123456.789 as 123.456,78 in python 2.7 without using locale? Thousand separator should be DOT instead of COMMA and decimal separator should be COMMA

Solution 1:

If you really, really want to avoid locale then you could work with the value returned from format(val, ',') and then swap the , and .:

>>>a = 1234567.89>>>from string import maketrans>>>trans = maketrans('.,', ',.')>>>format(a, ',.2f').translate(trans)
'1.234.567,89'

Solution 2:

One way is to change your locale:

>>>import locale>>>locale.setlocale(locale.LC_ALL, 'deu_DEU')
'German_Germany.1252'
>>>"{0:n}".format(12345.67)
'12.345,7'
>>>locale.setlocale(locale.LC_ALL, '')
'English_United Kingdom.1252'
>>>"{0:n}".format(12345.67)
'12,345.7'
>>>

Post a Comment for "Using Dot As Thousand Separator In Python2.7"