473,323 Members | 1,574 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,323 software developers and data experts.

Calculate formula from string?

Hi!

I am currently converting an ASP/SQL Server application to VB.Net/Access.
The application performs calculations based on which products are selected.
Therefore i need to be able to store formulas in the database, replace
placeholdes with values and execute the calculation.

Earlier this was done by executing the formula on SQL Server and retrieving
the result, but that is no longer possible - or desireable for that matter.

So if I have a string that says "2*2" how to I execute this and get the
result back?

(which my sharp brain tells me should be somewhere around 4) ;)

Thanks in advance

- Klaus
Nov 20 '05 #1
4 9932
I don't know ASP.Net but if it supports VB Script then there is a function
called 'Eval' ( and also Execute) which does exactly what you are looking
for.
"Klaus Jensen" <msnews2@icatech..dk> wrote in message
news:el*************@TK2MSFTNGP10.phx.gbl...
Hi!

I am currently converting an ASP/SQL Server application to VB.Net/Access.
The application performs calculations based on which products are selected. Therefore i need to be able to store formulas in the database, replace
placeholdes with values and execute the calculation.

Earlier this was done by executing the formula on SQL Server and retrieving the result, but that is no longer possible - or desireable for that matter.
So if I have a string that says "2*2" how to I execute this and get the
result back?

(which my sharp brain tells me should be somewhere around 4) ;)

Thanks in advance

- Klaus

Nov 20 '05 #2
Hi Klaus,

|| I have a string that says "2*2" ...
|| (which my sharp brain tells me should be somewhere around 4) ;)

You know, I think you're right! Have you ever thought of going into
programming? I think you'd be good at it. ;-)

Here's one implementation of an arithmetic evaluator class.
http://www.palmbytes.de/content/dotnetlibs/mathlib.htm

The site is in German but the Download link is obvious and everything's in
English in
the zip file.The app is about showing a graph based on user-defined equations.
Have a dig around inside the evaluator. You'll probably have to do a bit of
work getting your calculator variables set up, ditto additional functions.
Then you're off!

If you want full-on calculation - anything that VB.NET can calculate - you
might like to explore the VSA Namespace. This is the realm of the scripting
engine and can handle some powerful code. Naturally it's a lot more effort to
get going. There is a newsgroup just for this topic:
microsoft.public.dotnet.vsa, though it's not hugely busy.

Another option you could explore is dynamic compilation. I haven't looked
at that myself, so my paragraph runs out here.

Out of interest, could you post some examples of the expressions that
you'd like to evaluate?

Have fun. ;-)

Regards,
Fergus
Nov 20 '05 #3
"Fergus Cooney" <fi******@tesco.net> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
If you want full-on calculation - anything that VB.NET can calculate - you
might like to explore the VSA Namespace. This is the realm of the scripting engine and can handle some powerful code. Naturally it's a lot more effort to get going. There is a newsgroup just for this topic:
microsoft.public.dotnet.vsa, though it's not hugely busy.
This seems to be the best bet, also because this would possibly be a
valuable .net lesson I might be able to use later. :)
Out of interest, could you post some examples of the expressions that
you'd like to evaluate?


Well, okay then.

5,0722611E-13*x*x*x*x - 6,82931882931E-09*x*x*x +
0,0000343139083138486*x*x - 0,0762394457392556*x + 74,1895493393322

Hope your're a happy trooper now! :D

Thanks for your help. :)

- Klaus


Nov 20 '05 #4
Hi Klaus,

Thanks for the expression. I'm trooping off to calculate it right now,
lol.

I'm interested in expression evaluators and am currently converting an old
one that I wrote for Dos. It had functions like File("C:\Foo.txt").Line(3) =
"Whatever" and RunProg ("Dir *.*").Contains (" 0 Files").

I was just wondering whether you had any functions or anything that might
give me additional food for thought.

Regards,
Fergus


Nov 20 '05 #5

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

Similar topics

2
by: Gálos Zsuzsa | last post by:
Hi all, I need to calculate Formulas. For example: dim strFormula as string="25*(12-6)" I ned a DotNet Function to calculate this Formula (25*(12-6)=150): dim dblValue as double = 150
1
by: Don Leverton | last post by:
Hi Folks, I *can* calculate the Gross Profit Percentage where both the Cost and SellPrice are known, using the formula: (SellPrice - Cost) / SellPrice = GPP eg ($24.92 - $14.95) / $24.92 =...
2
by: Gálos Zsuzsa | last post by:
Hi all, I need to calculate Formulas. For example: dim strFormula as string="25*(12-6)" I ned a DotNet Function to calculate this Formula (25*(12-6)=150): dim dblValue as double = 150
8
by: VladimirT | last post by:
I have TextBox in him User enters the formula for instance Cos(x) what compute in code of the program? she is got text (( many thanks
2
by: Ivan | last post by:
Hi All, I just wanna ask one simply question.. Is there anyone know how to calculate the output value from a string like that?? Dim strFormula As String = "20+30+(100+20+30)*0.1" Dim...
12
by: james121285 | last post by:
This is my first ever program. I have struggled big time and its token so long my university lab is closed, can someone please check this over and maybe even correct some parts. I would be so happy...
7
by: mturner64 | last post by:
I need the formula to calculate the girth of a cone. I am not having any luck. Anyone know the formula? Thanks, Mike
4
by: trogenone | last post by:
Hi i have already asked what to use for calculating an age and that works fine. The problem is it dosnt leave a value in the desired table. Tables: Employees & Holidays I asked for a formula...
7
by: Wiredboy | last post by:
Hi, i'm pretty new in this and i hope somebody can help me with this: i have a db with 5 columns with different values and i want to calculate the average of them. I've tried to create a formula...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.