Click here to Skip to main content
Click here to Skip to main content
Go to top

MathParser - Math Formula Parser

, 20 Jan 2008
Rate this:
Please Sign up or sign in to vote.
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();
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)

Share

About the Author

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

Comments and Discussions

 
GeneralThank you ! PinmemberkHALEELus128-Apr-14 7:40 
QuestionThank you for ur sharing Pinmemberyongbam11-Aug-13 21:49 
QuestionHello PinmemberMember 92023892-Jul-12 21:57 
AnswerRe: Hello PinmemberMember 1002020530-Apr-13 6:41 
QuestionThank you Pinmemberdwmills27-Oct-11 14:49 
GeneralBug PinmemberMr.PoorEnglish2-Apr-11 7:16 
GeneralRe: Bug PinmemberDamir127-Jun-11 22:34 
GeneralRe: Bug PinmemberJWillmott1236-Jul-11 3:00 
GeneralTHANKS! Pinmembermattan20002-Jan-11 9:46 
GeneralMy vote of 5 Pinmembermattan20002-Jan-11 9:44 
General1/2 and 1.0/2 Pinmemberjiming liu26-Dec-10 8:02 
Generalthanks Pinmemberva12310-Sep-10 6:33 
Generaldispite of correct term -> exception (negative numbers) Pinmemberxparet020923-Oct-09 12:12 
GeneralRe: dispite of correct term -> exception (negative numbers) PinmemberJWillmott1236-Jul-11 3:03 
GeneralNOT WORK Pinmemberdataminers10-Jul-09 7:43 
GeneralRe: NOT WORK Pinmemberonurr30-Aug-09 12:50 
GeneralThanks PinmemberGoDeep11-May-09 4:09 
GeneralThank you PinmemberTomas Hradec21-Jan-09 22:15 
GeneralSome Bugs PinmemberMember 285390016-Jul-08 10:47 
GeneralRe: Some Bugs Pinmemberxginox13-Nov-08 14:33 
Generalthe download project doesn't work Pinmemberbig_onion20-Jan-08 17:20 
GeneralRe: the download project doesn't work PinmemberTamer Oz20-Jan-08 17:32 
GeneralRe: the download project doesn't work Pinmemberbig_onion20-Jan-08 18:20 
GeneralAlgorithm used PinmemberImtiaz A20-Jan-08 11:09 

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

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web01 | 2.8.140926.1 | Last Updated 20 Jan 2008
Article Copyright 2008 by Tamer Oz
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid