我们从Python开源项目中,提取了以下9个代码示例,用于说明如何使用locale.atof()。
def float(self, val): """ Parse a string to a floating point number. Uses locale.atof(), in future with ICU present will use icu.NumberFormat.parse(). """ try: return locale.atof(val) except ValueError: point = locale.localeconv()['decimal_point'] sep = locale.localeconv()['thousands_sep'] try: if point == ',': return locale.atof(val.replace(' ', sep).replace('.', sep)) elif point == '.': return locale.atof(val.replace(' ', sep).replace(',', sep)) else: return None except ValueError: return None #------------------------------------------------------------------------- # # Translations Classes # #-------------------------------------------------------------------------
def get_float(s10) : try : return float(s10) except : try : return locale.atof(s10) except : return 0.0
def compute(self, input_string): while input_string.count('(') > input_string.count(')') : input_string = input_string + ')' self.opened_paren = 0 self.save_edit = input_string for i in('-', '+', '/', '*', '(', ')'): input_string = input_string.replace(i, " %s " % i) input_string = input_string.replace('Pi', str(math.pi)) qualified = '' for i in input_string.split(): try: i = str(locale.atof(i)) qualified = qualified + str(float(i)) except: qualified = qualified + i try : return True, eval(qualified) except : return False, 0.0
def cast(self, value): try: return super(DecimalType, self).cast(value) except DefaultCastNotMacthed: pass try: return decimal.Decimal(value) except: value = locale.atof(value) if sys.version_info < (2, 7): value = str(value) return decimal.Decimal(value)
def current_specified_assets(self): amount_str = self("editor_currentassets_entry").get_text() try: # try to convert to float to_float = locale.atof(amount_str) except: # didn't work to_float = 0 return to_float ############### ### Methods ### ###############
def format_amount_entry(self, entry): amount_str = entry.get_text() wrongstr = _("This is not a valid amount!") try: to_float = locale.atof(amount_str) formatted_str = locale.currency(to_float, grouping=True,symbol=False) entry.set_icon_from_stock(Gtk.EntryIconPosition.PRIMARY,None) entry.set_text(formatted_str) entry.set_tooltip_text(_("Your current total assets")) except ValueError: entry.set_icon_from_stock(Gtk.EntryIconPosition.PRIMARY, Gtk.STOCK_STOP) entry.set_icon_tooltip_text(Gtk.EntryIconPosition.PRIMARY,wrongstr) entry.set_tooltip_text(wrongstr)
def cast(self, value): if value in ('', None): return None try: return decimal.Decimal(value) except: value = locale.atof(value) if sys.version_info < (2, 7): value = str(value) return decimal.Decimal(value)
def toFloat(s): if len(s)>=3 and s[-3]==',': s[-3] = '.' return locale.atof(s) # quick convert of numeric list to list of strings
def read_price(str_bid): ''' Read bid as string, return as float. Take care of localization ''' return locale.atof(str_bid)