Hello,
If I understand well your request, this is a function that may help you,
or give
an idea how this can be done. I'm still a beginner with all this stuff,
so ... be
careful. It does use locale to find out which separator character should
be used
(as one part of the world is using ' and not ,). You would call the
function
from the EndEdit method of your cell editor class.
Example:
#-----------------------------------------------
...
import locale
...
# Treat locale (getting the character for thousands separator)
locale.setlocale(locale.LC_ALL, '') # It is required to override the default
thousep = locale.localeconv() # Creates a dictionary with all
locale infos in
thousands_sep = thousep['thousands_sep']
...
...
class myCellEditor(wx.grid.PyGridCellEditor):
def __init__(self, thousands_sep):
wx.grid.PyGridCellEditor.__init__(self)
self.thousands_sep = thousands_sep
...
def EndEdit(self, row, col, grid):
self.value = groupChars(self.value, self.thousands_sep) # Runs
thousands seperator thing
...
...
def groupChars(value, thousands_sep):
''' Function = groupChars, args = value, thousands_sep
Add either a ' or a , each 3 characters.
This is used for large numbers typically entered in a numeric field.
Example: entering 1234567.89 will return 1,234,567.89 depending
on the value of thousands_sep.
value: must be a string
thousands_sep: should be either , or ', depending on the locale.
locale should be defined at the begining of the
application.
returns: a string in the here above described format.
WARNING! the function doesn't check that "value" is a number.
the function doesn't change the decimal . or ,
that varies from locale
'''
parts = str(value).split('.')
value = parts[0]
chunks = []
while len(value) > 0:
if len(value) > 3:
chunks.insert(0, value[-3:])
value = value[:-3]
else:
chunks.insert(0,value)
value = ''
parts[0] = thousands_sep.join(chunks) # thousands_sep comes from locale
return '.'.join(parts) # not supporting decimal
locale, using .
#----------------------------------------------------------------
Hope this helps,
Raphael