Re: convert user input to Decimal objects using eval()?
On Tuesday 29 March 2005 03:04, Raymond Hettinger wrote: > from decimal import Decimal > import re > > number = re.compile(r"((\b|(?=\W))(\d+(\.\d*)?|\.\d+)([eE][+-]?\d{1,3})?)") > deciexpr = lambda s: number.sub(r"Decimal('\1')", s) > > for s in ('1.0001+0.111', > '+21.3e-5*85-.1234/81.6', > '1.0/7'): > print '%s\n --> %r' % (s, eval(s)) > s = deciexpr(s) > print '%s\n --> %r\n' % (s, eval(s)) Wow! Thank you so much!!! now I can do my simple math function evaluator much more reliable ! Thanks again! -- Julián -- http://mail.python.org/mailman/listinfo/python-list
convert user input to Decimal objects using eval()?
Hi ! This is maybe a silly question, but... is there a "easy way" to make eval() convert all floating numbers to Decimal objects and return a Decimal? for example: eval('1.0001+0.111') --> convert each number to a Decimal object, perform the sum and obtain a Decimal object as a result? maybe a parser is needed, but eval() already do the job, but I need the precision that Decimal offers for numerical applications. Thanks in advance. -- Julián -- http://mail.python.org/mailman/listinfo/python-list