convert user input to Decimal objects using eval()?

Julian Hernandez Gomez jhernandez at pragma.com.co
Tue Mar 29 09:22:15 EST 2005


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.00000001+0.1111111',
>    '+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



More information about the Python-list mailing list