So far, this library provides the following calculators to be used to calculate mathematical expressions:
PrattCalculator is a C# library for evaluating mathematical expressions using a Pratt parser. It supports standard arithmetic operations, parentheses, exponentiation, unary minus, implicit multiplication, and decimal numbers. The calculator also allows specifying the number of decimal places for the result.
- Basic arithmetic:
+,-,*,/ - Exponentiation:
^ - Parentheses for grouping:
( … ) - Unary minus:
-5 - Implicit multiplication:
2(3+4)→2*(3+4) - Decimal numbers:
3.14,0.5 - Customizable decimal precision: round results to a specified number of decimals
Copy the PrattCalculator class into your project, or include it via your preferred project structure.
using StringCalculator.Types;
var calc = new PrattCalculator(); // default 2 decimal places
double result = calc.Calculate("2 + 3(2*2^4)5(3/1)");
Console.WriteLine(result); // outputs the evaluated result rounded to 2 decimals- -2.5*(1.1 + 2.2(3.3^2)) + 4.4*(5.5 - 3.3)
- 3.3*(2.5^1.5 + 1.2)*(4.0/2.0) - 5.1
- 1.5 + 2.2(3.1 + 4.5)1.2(5.0 - 2.5)