0

I have a number with 1 decimal point such as 123456.1 i would like to format it to 123,456.1

Tried to use locale to format the number but was unable to get it to work

Instead I used the following:

def format(n):
    r = []
    for i, c in enumerate(reversed(str(n))):
        if i and (not (i % 3)):
            r.insert(0, ',')
        r.insert(0, c)
    return ''.join(r)

which results in 1,234,56.1

kayduh
  • 308
  • 1
  • 5
  • 22

1 Answers1

0

Use string formatting.

>>> '{:,}'.format(123456.1)
'123,456.1'
TigerhawkT3
  • 48,464
  • 6
  • 60
  • 97