~openerp-dev/openobject-client/page_tooltips

« back to all changes in this revision

Viewing changes to bin/tools/user_locale_format.py

  • Committer: Naresh(OpenERP)
  • Date: 2011-02-01 09:57:52 UTC
  • mfrom: (1369.10.109 client)
  • Revision ID: nch@tinyerp.com-20110201095752-dfjlb0apnmbqi6jo
[MERGEĀ FROMĀ TRUNK]

Show diffs side-by-side

added added

removed removed

Lines of Context:
195
195
        if grouping:
196
196
            formatted = group(formatted, monetary=monetary, grouping=lang_grouping, thousands_sep=thousands_sep)[0]
197
197
    return formatted
 
198
 
 
199
def str2int(string):
 
200
    ''' Converts a string to an integer according to the locale settings
 
201
        that the user has in Administration/Translations/Languages.
 
202
    '''
 
203
    assert isinstance(string, basestring)
 
204
    return str2float(string, int)
 
205
 
 
206
def str2float(string, func=float):
 
207
    ''' Parses a string as a float according to the locale settings
 
208
    that the user has in Administration/Translations/Languages.
 
209
    '''
 
210
    assert isinstance(string, basestring)
 
211
    try:
 
212
        #First, get rid of the thousand separator
 
213
        ts = LOCALE_CACHE.get('thousands_sep')
 
214
        if ts:
 
215
            string = string.replace(ts, '')
 
216
        #next, replace the decimal point with a dot
 
217
        dd = LOCALE_CACHE.get('decimal_point')
 
218
        if dd:
 
219
            string = string.replace(dd, '.')
 
220
        #finally, parse the string
 
221
        return func(string)
 
222
    except:
 
223
        type = 'float'
 
224
        if func == int:
 
225
            type = 'integer'
 
226
        raise ValueError("%r does not represent a valid %s value" % (string,type))