0

I have a simple question how can i show the number 12045678 as 12,045,678 i.e automatically show in american format in jython

so 12345 should be 12,345 and 1234567890 should be 1,234,567,890 and so on.

Thanks everyone for your help.

Nick Fortescue
  • 43,045
  • 26
  • 106
  • 134
kdev
  • 291
  • 1
  • 8
  • 21
  • Added vote to close because it's a duplicate of the question I left a link to in my answer http://stackoverflow.com/questions/1823058/how-to-print-number-with-commas-as-thousands-separators-in-python-2-x – Nick Fortescue Dec 17 '09 at 13:26

4 Answers4

1

See the official documentation, in particular 7.1.3.1. Format Specification Mini-Language and in particular:

'n' Number. This is the same as 'g', except that it uses the current locale setting to insert the appropriate number separator characters.

Andreas Bonini
  • 44,018
  • 30
  • 122
  • 156
0

See How to print number with commas as thousands separators?

Community
  • 1
  • 1
Nick Fortescue
  • 43,045
  • 26
  • 106
  • 134
0

You can use a function like this:

def numberToPrettyString(n):
   """Converts a number to a nicely formatted string.
      Example: 6874 => '6,874'."""
   l = []
   for i, c in enumerate(str(n)[::-1]):
       if i%3==0 and i!=0:
           l += ','
       l += c
   return "".join(l[::-1])
Jabba
  • 19,598
  • 6
  • 52
  • 45
0

prior to 2.6 there is no built-in function

this is the easiest one I've found

def splitthousands(s, sep=','):  
    if len(s) <= 3: return s  
    return splitthousands(s[:-3], sep) + sep + s[-3:]

splitthousands('123456')

123,456

Ron
  • 296
  • 1
  • 6