12,956,777 members (52,426 online)
alternative version

#### Stats

94K views
78 bookmarked
Posted 20 Jan 2008

# MathParser - Math Formula Parser

, 20 Jan 2008 CPOL
 Rate this:
MathParser is a class that can calculate the result of mathematic operations given as string such as "3+5/2-3*(1+3)". It can also calculate the result of parametric formulas such as 3A+5B/2C. Calculation is made by operation order(/,*,-,+) and parenthesis rule.

## Introduction

`MathParser` is a class that can calculate the result of mathematic operations given as `string `such as "`3+5/2-3*(1+3)`". It can also calculate the result of parametric formulas such as `3A+5B/2C`. Calculation is made by operation order(/,*,-,+) and parenthesis rule. In some projects, users want to create a formula dynamically. This is a useful method for calculating the values of these types of formulas.

## Background

The basic idea of this code is parsing the `string `correctly, converting values to decimal and calculating them with the operator.

## Using the Code

Using the code is pretty simple.

For basic use, developers should call the `calculate `method by passing the formula.

```MathFunctions.MathParser mp = new MathFunctions.MathParser();
decimal result = mp.Calculate("5/(1-2)+(3+(2/1))");```

For parametric use, developers should declare parameters before calling the `calculate `method, then call the method passing the formula as seen below:

```MathFunctions.MathParser mp = new MathFunctions.MathParser();
decimal result = mp.Calculate("3D-2B/C+(A-B)");```

## History

• 20.01.2007: Initial release
For bug reports and suggestions, feel free to contact me at oztamer@hotmail.com.

## Share

Tamer Oz is a Microsoft MVP and works as Assistant Unit Manager.

## You may also be interested in...

 First Prev Next
 Thank you ! kHALEELus128-Apr-14 7:40 kHALEELus1 28-Apr-14 7:40
 Thank you for ur sharing yongbam11-Aug-13 21:49 yongbam 11-Aug-13 21:49
 Hello Member 92023892-Jul-12 21:57 Member 9202389 2-Jul-12 21:57
 Re: Hello Member 1002020530-Apr-13 6:41 Member 10020205 30-Apr-13 6:41
 Thank you dwmills27-Oct-11 14:49 dwmills 27-Oct-11 14:49
 Bug Mr.PoorEnglish2-Apr-11 7:16 Mr.PoorEnglish 2-Apr-11 7:16
 Re: Bug Damir127-Jun-11 22:34 Damir1 27-Jun-11 22:34
 Re: Bug JWillmott1236-Jul-11 3:00 JWillmott123 6-Jul-11 3:00
 THANKS! mattan20002-Jan-11 9:46 mattan2000 2-Jan-11 9:46
 My vote of 5 mattan20002-Jan-11 9:44 mattan2000 2-Jan-11 9:44
 1/2 and 1.0/2 jiming liu26-Dec-10 8:02 jiming liu 26-Dec-10 8:02
 thanks va12310-Sep-10 6:33 va123 10-Sep-10 6:33
 dispite of correct term -> exception (negative numbers) xparet020923-Oct-09 12:12 xparet0209 23-Oct-09 12:12
 Re: dispite of correct term -> exception (negative numbers) JWillmott1236-Jul-11 3:03 JWillmott123 6-Jul-11 3:03
 NOT WORK dataminers10-Jul-09 7:43 dataminers 10-Jul-09 7:43
 Re: NOT WORK onurr30-Aug-09 12:50 onurr 30-Aug-09 12:50
 Thanks GoDeep11-May-09 4:09 GoDeep 11-May-09 4:09
 Some Bugs Member 285390016-Jul-08 10:47 Member 2853900 16-Jul-08 10:47
 Re: Some Bugs xginox13-Nov-08 14:33 xginox 13-Nov-08 14:33
 Re: the download project doesn't work Tamer Oz20-Jan-08 17:32 Tamer Oz 20-Jan-08 17:32