This question already has answers here:
Best and shortest way to evaluate mathematical expressions
(8 answers)
Evaluating string "3*(4+2)" yield int 18 [duplicate]
(13 answers)
Closed 9 years ago.
Guys I have to evaluate a mathematical expression(which is represented by a string).
Till now I had simple expressions thus the following was working fine
var result = new DataTable().Compute(STRING_HERE, null);
However now my string expression is becoming a bit more complex and the above method is starting to give errors.
Any idea of how I can handle this situation ?
Please I would like some kind of inbuilt method or function like apporach(preferably).
Not inbuild, but NCalc (http://ncalc.codeplex.com/) is a very nice library for evaluating math expressions.
Related
This question already has answers here:
How can I check if a string contains a character in C#?
(8 answers)
Closed 5 years ago.
I want to check if an inputted string contains a question mark.
Probably quite simple but I'm new to coding.
Use String.Contains() :
string myString = "Hello world?";
bool containsQuestionMark = myString.Contains("?"); // true
For future references, use MSDN, it's filled with good documentation.
Alternatively (to Rick's answer), if you are checking just for char occurance in a string you can use IndexOf(char):
bool containsQuestionMark = myString.IndexOf('?') != -1;
There are also some minor (negligible) performance differences between two approaches, depending on the framework version being used.
This question already has answers here:
String Interpolation with format variable
(7 answers)
Closed 7 years ago.
Has anyone figured out how to reuse an interpolated string?
I.e. can anyone figure out how to do away with the string.Format in the following block of code?
foreach(var s in new[]{ "Primary_{0}_Home", "Secondary_{0}_Work" }){
sql = $"SELECT {string.format(s, "Street")}, {string.format(s, "City")} ..."
}
You can't do that.
Interpolated strings are set on compile time. You can't use string interpolation to load a string to format something not directly in the scope.
This question already has answers here:
How can I convert an integer into its verbal representation?
(16 answers)
Closed 7 years ago.
How can I convert final amount of billing to it's related vocabulary form?
I am trying to develop a function which convert it in vocabulary form.
For, ex. 12345 Rs. then it's output should be like this
TWELVE THOUSAND THREE HUNDRED N FORTY FIVE ONLY
is it possible with few easy steps in C# ?
If you are willing to use an external library, i'd suggest you try out Humanizer
Really simply to use and your example would be as simple as.
12345.ToWords(); //it adds extension methods that achieve this.
This question already has answers here:
What's the main difference between int.Parse() and Convert.ToInt32
(13 answers)
Closed 9 years ago.
I would like to know if there is any difference between the captioned two methods?
string str = "14.75";
Console.WriteLine(Decimal.Parse(str));
Console.WriteLine(Convert.ToDecimal(str));
Can anyone please let me know? Thanks.
Decimal.Parse only supports parsing string values, whereas Convert takes other types as well, like, object, int, byte, DateTime etc.
This question already has answers here:
How can I evaluate a math expression represented by a string?
(6 answers)
Closed 9 years ago.
Is there any method in C# in which you will pass a mathematical statement, and that method will give you the result for example you will pass this string
1+2-3/4*5
and it will give you in return
0
Use NCalc
Expression e = new Expression("2 + 3 * 5");
Debug.Assert(17 == e.Evaluate());