Click here to Skip to main content
Click here to Skip to main content

MathParser - Math Formula Parser

By , 20 Jan 2008
 

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();
mp.Parameters.Add(MathFunctions.Parameters.A, 5);
mp.Parameters.Add(MathFunctions.Parameters.B, 2);
mp.Parameters.Add(MathFunctions.Parameters.C, 1);
mp.Parameters.Add(MathFunctions.Parameters.D, 3);
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.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Tamer Oz
Team Leader
Turkey Turkey
Member
Tamer Oz is a Microsoft MVP and works as Assistant Unit Manager.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
QuestionHellomemberMember 92023892 Jul '12 - 21:57 
AnswerRe: HellomemberMember 1002020530 Apr '13 - 6:41 
QuestionThank youmemberdwmills27 Oct '11 - 14:49 
GeneralBugmemberMr.PoorEnglish2 Apr '11 - 7:16 
GeneralRe: BugmemberDamir127 Jun '11 - 22:34 
GeneralRe: BugmemberJWillmott1236 Jul '11 - 3:00 
GeneralTHANKS!membermattan20002 Jan '11 - 9:46 
GeneralMy vote of 5membermattan20002 Jan '11 - 9:44 
General1/2 and 1.0/2memberjiming liu26 Dec '10 - 8:02 
Generalthanksmemberva12310 Sep '10 - 6:33 
Generaldispite of correct term -> exception (negative numbers)memberxparet020923 Oct '09 - 12:12 
GeneralRe: dispite of correct term -> exception (negative numbers)memberJWillmott1236 Jul '11 - 3:03 
GeneralNOT WORKmemberdataminers10 Jul '09 - 7:43 
GeneralRe: NOT WORKmemberonurr30 Aug '09 - 12:50 
GeneralThanksmemberGoDeep11 May '09 - 4:09 
GeneralThank youmemberTomas Hradec21 Jan '09 - 22:15 
GeneralSome BugsmemberMember 285390016 Jul '08 - 10:47 
GeneralRe: Some Bugsmemberxginox13 Nov '08 - 14:33 
Generalthe download project doesn't workmemberbig_onion20 Jan '08 - 17:20 
GeneralRe: the download project doesn't workmemberTamer Oz20 Jan '08 - 17:32 
GeneralRe: the download project doesn't workmemberbig_onion20 Jan '08 - 18:20 
GeneralAlgorithm usedmemberImtiaz A20 Jan '08 - 11:09 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web03 | 2.6.130516.1 | Last Updated 20 Jan 2008
Article Copyright 2008 by Tamer Oz
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid