473,382 Members | 1,784 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

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.00000001+0.1111111') --> 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
Jul 18 '05 #1
4 3864
[Julian Hernandez Gomez]
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?


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))

"""
1.00000001+0.1111111
--> 1.11111111
Decimal('1.00000001')+Decimal('0.1111111')
--> Decimal("1.11111111")

+21.3e-5*85-.1234/81.6
--> 0.016592745098039215
+Decimal('21.3e-5')*Decimal('85')-Decimal('.1234')/Decimal('81.6')
--> Decimal("0.01659274509803921568627450980")

1.0/7
--> 0.14285714285714285
Decimal('1.0')/Decimal('7')
--> Decimal("0.1428571428571428571428571429")

"""

Raymond Hettinger
Jul 18 '05 #2
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
Jul 18 '05 #3

"Raymond Hettinger" <vz******@verizon.net> wrote in message
news:6_72e.50077$u76.2569@trndny08...
[Julian Hernandez Gomez]
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?


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))

"""
1.00000001+0.1111111
--> 1.11111111
Decimal('1.00000001')+Decimal('0.1111111')
--> Decimal("1.11111111")

+21.3e-5*85-.1234/81.6
--> 0.016592745098039215
+Decimal('21.3e-5')*Decimal('85')-Decimal('.1234')/Decimal('81.6')
--> Decimal("0.01659274509803921568627450980")

1.0/7
--> 0.14285714285714285
Decimal('1.0')/Decimal('7')
--> Decimal("0.1428571428571428571428571429")


This is less obvious and more useful, to me, than some of the recipies in
the new Cookbook.

TJR

Jul 18 '05 #4
> > [Julian Hernandez Gomez]
is there a "easy way" to make eval() convert all floating
numbers to Decimal objects and return a Decimal?

[Raymond Hettinger] 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)

[Terry Reedy] This is less obvious and more useful, to me, than some of the recipies in
the new Cookbook.


Okay, we can fix that. I've cleaned it up a bit and posted it on ASPN with
references, docs, and a doctest:

http://aspn.activestate.com/ASPN/Coo.../Recipe/393265
Raymond
Jul 18 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Arvid Andersson | last post by:
Hello, I need to convert a string to a number, but the string can contain +,-,* and / as well as parenthesis. For example, if I have the string "30/(6+9)" I would like a function that returned the...
16
by: flyaflya | last post by:
a = "(1,2,3)" I want convert a to tuple:(1,2,3),but tuple(a) return ('(', '1', ',', '2', ',', '3', ')') not (1,2,3)
4
by: aevans1108 | last post by:
expanding this message to microsoft.public.dotnet.xml Greetings Please direct me to the right group if this is an inappropriate place to post this question. Thanks. I want to format a...
5
by: Learner | last post by:
Hello, Here is the code snippet I got strucked at. I am unable to convert the below line of code to its equavalent vb.net code. could some one please help me with this? static public...
2
by: Kathi215 | last post by:
hi, i`m wowrking in project that accept user input from swtiches(on /off) which is binary and then i have to display that number in lcd via maicrocontroller.I'm using c interactive language.My...
52
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - How do I convert a Number into a String with exactly 2 decimal places?...
7
by: elliotng.ee | last post by:
I have a text file that contains a header 32-bit binary. For example, the text file could be: %%This is the input text %%test.txt Date: Tue Dec 26 14:03:35 2006...
3
by: mrajanikrishna | last post by:
Hi Friends, I am accepting a number from the user entered in a textbox. I want to assign to a variable in my code and assignt this to that variable. double num1 = (double)txtNum1.text; ...
2
karthickbabu
by: karthickbabu | last post by:
Hi In my application i want to convert integer to decimal. I get a input and using convert function to convert into decimal. But it shows as it self. My code like as below, Is any wrong in...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.