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

How to evaluate a math expression contained in a string?


If I have a string with a valid math expression like " (2 + 28)/1", how can
I evaluate it?

Thanks,

Juan.

Nov 16 '05 #1
4 18331
You can always work hard and write it yourself, but I think you are not the
first to meet this. I found something in CodeProject:
http://www.codeproject.com/vb/net/ma..._evaluator.asp
I you search yourself, I'm sure you'll find more.

"Juan" wrote:

If I have a string with a valid math expression like " (2 + 28)/1", how can
I evaluate it?

Thanks,

Juan.

Nov 16 '05 #2
Try the .Math library:
http://hebertsoft.com/dotmath/ (Note, he's moving the downloads from
http://workspaces.gotdotnet.com/math to
https://sourceforge.net/projects/dotmath/, but is still in the process)

"Juan" <ju*****************@ANTISPAMhotmail.com> wrote in message
news:uS**************@TK2MSFTNGP10.phx.gbl...

If I have a string with a valid math expression like " (2 + 28)/1", how can I evaluate it?

Nov 16 '05 #3
I'm not sure how safe this is, but I used this in a non-production
environment and it works quite well:
"Amiram Korach" <Am**********@discussions.microsoft.com> wrote in message
news:64**********************************@microsof t.com...
You can always work hard and write it yourself, but I think you are not
the
first to meet this. I found something in CodeProject:
http://www.codeproject.com/vb/net/ma..._evaluator.asp
I you search yourself, I'm sure you'll find more.

"Juan" wrote:

If I have a string with a valid math expression like " (2 + 28)/1", how
can
I evaluate it?

Thanks,

Juan.

Nov 16 '05 #4
Sorry about that, here's the code:

------------------------------------------------------------------------------
using System;

using Microsoft.JScript;
using Microsoft.JScript.Vsa;

namespace VPDBUtils
{
public class JScriptEvaluator
{
private static VsaEngine vsaEngine;

public static void Initialize()
{
vsaEngine = VsaEngine.CreateEngine();
}

public static void Close()
{
if (vsaEngine != null)
{
vsaEngine.Close();
}
}

private static object EvaluateString(string sStringToEvaluate)
{
return Eval.JScriptEvaluate(sStringToEvaluate, vsaEngine);
}

internal static double EvaluateStringAsDouble(string sStringToEvaluate)
{
return System.Convert.ToDouble(EvaluateString(sStringToEv aluate));
}
}
}
------------------------------------------------------------------------------
First you call the static Initialise, the pass your string to
EvaluateStringAsDouble() and it gives you the result back as a double.
"Amiram Korach" <Am**********@discussions.microsoft.com> wrote in message
news:64**********************************@microsof t.com...
You can always work hard and write it yourself, but I think you are not
the
first to meet this. I found something in CodeProject:
http://www.codeproject.com/vb/net/ma..._evaluator.asp
I you search yourself, I'm sure you'll find more.

"Juan" wrote:

If I have a string with a valid math expression like " (2 + 28)/1", how
can
I evaluate it?

Thanks,

Juan.

Nov 16 '05 #5

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

Similar topics

1
by: Faraz | last post by:
Hi everyone, I am developing a WinForms application and I need a Math Expression validator, not an expression evaluator. So I want to be able to say... if ((sales - tax) > 20) THEN (x * y *...
0
by: Faraz | last post by:
Hi everyone, I am developing a WinForms application and I need a Math Expression validator, not an expression evaluator. So I want to be able to say... if ((sales - tax) > 20) THEN (x * y *...
3
by: Jason luo | last post by:
Hi all, In c99-standard page 52,there is a sentence about void,as below: If an expression of any other type is evaluated as a void expression, its value or designator is discarded. I don't...
13
by: Zeng | last post by:
Hello, Please help!!! I've been stuck on this issue for months. I just wonder if there is a way to programmatically evaluate expression strings such as ( ( 3 + 5 ) / 2 ) > 4 --> this...
0
by: Faraz | last post by:
Hi everyone, I am developing a WinForms application and I need a Math Expression validator, not an expression evaluator. So I want to be able to say... if ((sales - tax) > 20) THEN (x * y *...
2
by: Faraz | last post by:
Thanks Dave, To repeat the question: I am developing a WinForms application and I need a Math Expression validator, not an expression evaluator. So I want to be able to say... if ((sales - tax)...
2
by: EDom | last post by:
Hi, I have expressions in a string like "100/24*5" and so on. How can I evaluate this expression to get the result. Regards, Vineet
3
by: Andrew Elmore | last post by:
How can in read the values of a textbox and translate it to a math equation? like the calc. When the box validates if there is a decimal it rejects it and goes back to what it was (e.cancel) How...
9
by: Bob M | last post by:
If I define an expression or equation, how can I retrieve that expression as string literal? I want to do this so that I could avoid repetitive typing (or copy/paste/change) the same thing at two...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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:
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...
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.