Grouping numeric characters in a grid cell

Another one, would be :

import re

def AddCommas(rep) :
    Mt = str(rep).split('.')
    Mt[0] = re.sub(r'(?<=\d)(?=(\d\d\d)+(\.|$))', ',', Mt[0])
    return '.'.join(Mt)

print AddSpace(12345.69)

Brian Wolf a écrit :

···

Below is another possible solution:

def AddCommas(n):
   parts = str(n).split('.')
   n = parts[0]
   chunks =
   while len(n) > 0:
       if len(n) > 3:
           chunks.insert(0, n[-3:])
           n = n[:-3]
       else:
           chunks.insert(0,n)
           n = ''
   parts[0] = ','.join(chunks)
   return '.'.join(parts)

print AddCommas(178143239.61)
print AddCommas(2354.)
print AddCommas(897)

178,143,239.61
2,354.0
897

Michael Moriarity wrote:

Hello Ralph:

On Dec 4, 2007 12:26 PM, Raph <maygeo@netplus.ch> wrote:

I'm displaying numeric values in grid cells (wx.grid and
wx.grid.PyGridTableBase). It looks like this:
23456789.43
and for readibility reasons, I'd like it to look like this:
23'456'789.43 or 23,456,789.43
    
I wrote a function a while ago to convert and integer into a string with commas
every 3 digits. It may be what you need.

def InsertNumberCommas(NumberInt):
    TempStr = '%d' % NumberInt
    NumDigits = len(TempStr)
    NumCommas = (NumDigits - 1) / 3
    NumberStr = ''
    Start = 0
    for Index in xrange(NumCommas, 0, -1):
        Stop = -Index * 3
        NumberStr += TempStr[Start:Stop] + ','
        Start = Stop
    NumberStr += TempStr[-3:]
    return NumberStr

---------------------------------------------------------------------
To unsubscribe, e-mail: wxPython-users-unsubscribe@lists.wxwidgets.org
For additional commands, e-mail: wxPython-users-help@lists.wxwidgets.org

---------------------------------------------------------------------
To unsubscribe, e-mail: wxPython-users-unsubscribe@lists.wxwidgets.org
For additional commands, e-mail: wxPython-users-help@lists.wxwidgets.org

--

Hugues JEAN-BAPTISTE (hjb@agorinfo.fr)
AGORINFO S.A.S. (http://www.agorinfo.fr)