简体   繁体   中英

C# calculate from string

I trying to calculate my string but I don't have any idea how to do that.

I want to do something like this:

string input = "2+2+2+2*5^2/2";
Console.WriteLine(Calculate(input));

I tried to use DataTable().Compute() but its dose not support many of operators.

You can use DataTable.Compute for that. this method computes the given expression on the current rows that pass the filter criteria.

You can read more about it here: https://docs.microsoft.com/en-us/dotnet/api/system.data.datatable.compute?view=net-5.0

string input = "2+2+2+2*5^2/2";
string res = new DataTable().Compute(input, null).ToString();

Console.WriteLine(res);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM