A simple parser/evaluator for math expressions in C#.
The library is built with a .NET Standard 2.0 version, but also versions for .NET Framework 3.5 and 4.5.
double result = Parser.Evaluate("(3+4)*-2");
If the expression cannot be evaluated (such as Parser.Evaluate("apple");
), it will throw a FormatException.
- Addition, subtraction, multiplication, division
- Exponents (
2^3
) - Decimals (
2*(0.3)
and2*(0,3)
(depends upon culture, see below)) - Implied multiplication (
(2-3)(4/6)
and3(4+6)
) - Negation (
-(4+5)
)
Some cultures use . for decimal separators (0.5) while others use , (0,5). The decimal separator used depends upon the culture the current thread is running on. If you want it one way or the other, you'll want to change the UI culture.
Learn which cultures use which symbol
This code is released under the MIT License.