NCalc specify type - c#

We have a generic calculation routine using Ncalc to evaluate a string. However we are running across a problem when the values in a multiplication are small enough that Ncalc sees them as int however the result is too big for an int.
Example:
var expr = new NCalc.Expression("1740263 * 1234");
object result = expr.Evaluate();
Console.WriteLine(result.ToString());
Console.ReadKey();
This results in a negative int value.
Is there any way to force Ncalc to use long for the calculation?
I have tried using parameters and this works but it would mean a major rewrite of our code to implement and I would like to avoid it if possible.
Thanks

NCalc uses Int32 as integer data type then you can't force a number to be calculated as Int64. However if you don't use built-in math functions and you rely on plain mathematical operators you may convert a number to long and it will invoke correct operators. Let's see it:
var expr = new NCalc.Expression("CLng(1740263) * 1234");
expr.EvaluateFunction += delegate(string name, NCalc.FunctionArgs args)
{
// Nostalgic CLng function...
if (String.Equals(name, "CLng", StringComparison.CurrentCultureIgnoreCase))
{
args.HasResult = true;
args.Result = Convert.ToInt64(args.EvaluateParameters()[0]);
}
};
Please note that you can't directly cast boxed Int32 parameter to Int64 then you have to use Convert.ToInt64() or double cast: (long)(int)args.EvaluateParameters()[0]. Now you can check result is correct:
var result = expr.Evaluate();
Console.WriteLine("Result is: {0} (type {1})",
result, result.GetType());
Proper type conversion is performed then you don't need to cast both values to long.
Note that you may also directly use floating point numbers (decimal in NCalc) and you won't have such problems:
var expr = new NCalc.Expression("1740263.0 * 1234.0");

You can use regex to extract all the number from the string, replace them with parameters and then set the parameters to the converted numbers. In this example I am replacing what would of been int numbers to decimal.
string myCalculationString = "1000000 * 10000000";
striny myPattern = #"-?\d+(,\d+)*(\.\d+(e\d+)?)?";
MatchCollection matches =
Regex.Matches(convertedCalculation, myPattern );
int matchCount = 0;
convertedCalculation = Regex.Replace(convertedCalculation,
myPattern , m => "[p" + matchCount++ + "]");
for (int i = 0; i < matches.Count; i++)
{
Match match = matches[i];
decimal number = Convert.ToDecimal(match.Value);
expression.Parameters.Add($"p{i}", number);
}
decimal result = (decimal)expression.Evaluate();
Note I have not tested the regular expression well but it seems to work for positive/negative numbers and allows optional decimal point. This will also throw an error if any of the numbers or the result is out of bounds for a decimal

Related

convert string to int error in c#

Hello I am trying to convert String to Integer.
The code below shows a part from where I am trying to convert my string to integer.
if (other.gameObject.CompareTag("PickUp"))
{
if ( checkpointboolean == false)
{
string pickupName = other.ToString(); //other = Pickup4
//Remove first 6 letters thus remaining with '4'
string y = pickupName.Substring(6);
print(y); // 4 is being printed
int x = 0;
int.TryParse(y, out x);
print (x); // 0 is being printed
I also tried the below code and instead of '0' I am getting the following error:
if (other.gameObject.CompareTag("PickUp"))
{
if ( checkpointboolean == false)
{
//Get Object name ex: Pickup4
string pickupName = other.ToString();
//Remove first 6 letters thus remaining with '4'
string y = pickupName.Substring(6);
print(y);
int x = int.Parse(y);
FormatException: Input string was not in the correct format
System.Int32.Parse (System.String s)
int.TryParse returns a boolean, true if it succeeded and false if not. You need to wrap this in a if block and do something with that logic.
if(int.TryParse(y, out x))
print (x); // y was able to be converted to an int
else
// inform the caller that y was not numeric, your conversion to number failed
As far as why your number is not converted I could not say until you post what the string value is.
Your first attempt is the best for this case, that code works fine, The int.TryParse() giving 0 to the out parameter and returns false means the conversion is failed. The input string is not in the correct format/ it cannot be converted to an integer. You can check this by using the return value of the int.TryParse(). For this what you need to do is :-
if(int.TryParse(y, out x))
print (x); //
else
print ("Invalid input - Conversion failed");
First of all, ToString() usually uses for debug purpose so there's no guarantee that
other.ToString()
will return the expected "Pickup4" in the next version of the software. You, probably, want something like
int x = other.gameObject.SomeProperty;
int x = other.SomeOtherProperty;
int x = other.ComputePickUp();
...
If you, however, insist on .ToString() you'd rather not hardcode six letters (the reason is the same: debug information tends to change from version to version), but use regular expressions or something:
var match = Regex.Match(other.ToString(), "-?[0-9]+");
if (match.Success) {
int value;
if (int.TryParse(match.Value, out value))
print(value);
else
print(match.Value + " is not an integer value");
}
else
print("Unexpected value: " + other.ToString());

Index of the string can be long?

I want to know whether we can give the index of the string as Long data type.
var i=long.Parse(Console.ReadLine());
var result = testString[i-1];
the second line giving me the error by saying that "The best overloaded method match for 'string.this[int]' has some invalid arguments."
No you can't use long for most collection types (you haven't specified what testString is).
One way to get around this would be to segregate the string into a multi-part / multi-dimension array then use a multiplier to get which part of the array to check.
For example:
Your index is 100,000 and you have an array of shorts (32,767 length)...
string[,] testString = new string[100, 32766]; //Replace this with your Initialisation / existing string
var arrayRank = (int)Math.Round((double) 100000 / 32767, 0);
var arrayIndex = (int)Math.Round((double)100000 % 32767, 0);
//Test this works.
//testString[arrayRank, arrayIndex] = "test"; - Test to see that the array range is assignable.
var result = testString[arrayRank, arrayIndex]; //Test value is what we expect
This may not be the most efficient way to go about things, but it is a workaround.
No, it cannot accept a long. The only overload accepts an int indexer. You would need to change your code to int.Parse() instead of long.Parse()
There is no way to pass long as an index of array, compiler doesn't allow that.
Workaround can be converting the long to int, this is called narrow conversion.
var result= testString[(int)i)];

Determine if a string is greater than another string

I have two strings
string A = "1.0.0.0";
string B = "1.0.0.1";
I need to evaluate somehow that B is greater than A (version wise) either converting those two strings to integers or decimals or something.
I tried the following
Decimal S = Convert.ToDecimal(A);
int S = Convert.ToInt32(A);
but keep getting the following error, "Input string was not in a correct format."
Any help will be appreciated.
See the Version Class.
You're able to do something like this:
Version a = new Version("1.0.0.0");
Version b = new Version("1.0.0.1");
if (b>a) //evaluates to true
blah blah blah
I haven't personally tested this exact scenario, but the Version class allows you to use comparison operators like I've shown here.
If your string has at most 4 numeric parts (separated by .), you can use the Version class to get a strongly typed entity that corresponds to these strings. Version implements the different comparison operators (==, >, < etc...) in the expected manner, so you can find out which is greater:
var a = new Version(A);
var b = new Version(B);
if(a > b)
// a is larger
else if (a < b)
// b is larger
else
// they are identical
If there are more than 4 parts, you will need to split each string to its numeric components, convert each one to a numeric equivalent and compare the two resulting collections.
Something like:
var aParts = A.Split('.');
var bParts = B.Split('.');
// assumes the string have the same number of parts
for(int i = 0; i < aParts.Length; i++)
{
var currA = int.Parse(aParts[i]);
var currB = int.Parse(bParts[i]);
if(currA == currB)
continue;
if(currA > currB)
// A is greater than B
else
// B is greater than A
}
You can take a look at System.Version class.
If the strings are in this format or can be converted to a version.
Version have comparers
If you want to compare version strings in .NET, then you can use the Version class.
Version version = new Version("1.0.0.0");
Version otherVersion = new Version(""1.0.0.1");
The class provides operators to check if a version is greater or lesser than another.
Split on the ".". Then convert each part to an int. Starting from the left: if A's fragment is lower, then report that A is first. If B's fragment is lower, then report that B is first. Otherwise, move to the next fragment. If you're at the last fragment already, report that they are equal.
If your strings have at most four parts (like version numbers), then as others suggested it's easier to use the System.Version class.
Instead of using VersionClass a fast approach would be something like this.
string A = "1.0.0.0";
string B = "1.0.0.1";
int versionA = Convert.ToInt32(A.Replace(".", string.Empty));
int versionB = Convert.ToInt32(B.Replace(".", string.Empty));
if (b>a)
//something will happen here
Replace changes the first string to the second one in this case string.Empty equals to "".

Convert a string to integer in C#/.NET [duplicate]

This question already has answers here:
How to convert string to integer in C#
(12 answers)
Closed 8 years ago.
I need to convert a string to integer. My string can be of any type (float/int/string/special character).
For example:
If my string is "2.3", I need to convert to = 2
If my string is "anyCharacter", I need to convert to = 0
If my string is "2", I need to convert to = 2
I tried the following:
string a = "1.25";int b = Convert.ToInt32(a);
I got the error:
Input string was not in a correct format
How do I convert it?
Use Double.TryParse() and once you get the value from it, convert it to int using Convert.ToInt():
double parsedNum;
if (Double.TryParse(YourString, out parsedNum) {
newInt = Convert.ToInt32(num);
}
else {
newInt = 0;
}
Try to parse it as a floating point number, and convert to integer after that:
double num;
if (Double.TryParse(a, out num) {
b = (int)num;
} else {
b = 0;
}
This should help: treat any string as if it were a double, then Math.Floor() it to round it down to the nearest integer.
double theNum = 0;
string theString = "whatever"; // "2.3"; // "2";
if(double.TryParse(theString, out theNum) == false) theNum = 0;
//finally, cut the decimal part
int finalNum = (int)Math.Floor(theNum);
NOTE: the if might not be needed per-se, due to theNum initialization, but it's more readable this way.
I think Convert.ToInt32 is the wrong place to look for - I would use Integer.Tryparse and if TryParse evaluates to false, assign a 0 to the variable. Before the TryParse, you could simply delete any character after the dot, if you find it in the string.
Also, keep in mind that some languages use "," as a separator.
Try:
if (int.TryParse(string, out int)) {
variable = int.Parse(string);
}
As far as I know, there isn't any generic conversion, so you'd have to do a switch to find out the type of the variable and then use either of the following (for each type):
int.Parse(string)
or
int.TryParse(string, out int)
The second one will return a boolean which you can use to see if the conversion passed or failed.
Your best option would be to use double or decimal parsing as this won't remove any decimal places, unlike int.
bool Int32.TryParse(string, out int)
The boolean return value indicates if the conversion was successful or not.
Try something like this:
public int ForceToInt(string input)
{
int value; //Default is zero
int.TryParse(str, out value);
return value;
}
This will do the trick. However I don't recommend taking this approach. It is better to control your input whereever you get it.

Bitwise operations on strings - 1440 characters length

How can i make bitwise operations on strings at c#
example
string sr1="0101110";
string sr2="1101110";
sr1 & sr2="0101110";
or
sr1 | sr2="1101110";
How can i make such comparison ?
Notice string lengths are fixed 1440 characters
Here my dirty solution
private string compareBitWiseAnd(string sr1, string sr2)
{
char[] crArray1 = sr1.ToCharArray();
char[] crArray2 = sr2.ToCharArray();
StringBuilder srResult = new StringBuilder();
for (int i = 0; i < crArray1.Length; i++)
{
if (crArray1[i] == crArray2[i])
{
srResult.Append(crArray1[i]);
}
else
{
srResult.Append('0');
}
}
return srResult.ToString();
}
private string compareBitWiseOr(string sr1, string sr2)
{
char[] crArray1 = sr1.ToCharArray();
char[] crArray2 = sr2.ToCharArray();
StringBuilder srResult = new StringBuilder();
for (int i = 0; i < crArray1.Length; i++)
{
if (crArray1[i] == '1' || crArray2[i] == '1')
{
srResult.Append("1");
}
else
{
srResult.Append('0');
}
}
return srResult.ToString();
}
Convert to actual bits first, and then do the bitwise comparison.
int num1 = Convert.ToInt32(sr1, 2);
int num2 = Convert.ToInt32(sr2, 2);
int result = num1 & num2;
Use this if you want to get a binary string from the result.
BigInteger is the type you are looking for. It also have BitwiseOr.
If you really need to stick with strings it is not very hard to compute bitwise operations on character-by-character basis... but I'd avoid doing it if possible.
And here is a question on how to construct BigInteger from string of any base - BigInteger Parse Octal String?
var bitString = "10101";
BigInteger value = bitString.Aggregate(new BigInteger(), (b, c) => b * 2 + c - '0');
You have to convert the string to numbers first, you can use "Convert.ToInt32(String, Int32)", the second parameter lets you specify the base:
string sr1 = "0101110";
string sr2 = "1101110";
int one = Convert.ToInt32(sr1, 2);
int two = Convert.ToInt32(sr2, 2);
int result = one & two;
hope it helps.
You can't do bitwise operations on a string in the way you intend. There are interesting things you can do with bitwise operations on strings with other goals, like changing their case, but I think this is what you want:
// Convert the string to an integer
int foo = Convert.ToInt32(sr1, 2);
int bar = Convert.ToInt32(sr2, 2);
// Perform binary styff
int result = foo & bar;
// Convert back to a string, if you want
string resultStr = result.ToString();
I like Alexei's BigInteger solution, but it does require .NET 4.0 minimum. If for some reason you can't use that, then another option is to use the BitArray class, which has been available since .NET 1.1. Unfortunately, there is no method built-in to BitArray to parse a binary string, so you have to do that manually, similar to Alexei's solution.
Another option is a class I wrote called BoolArray which does a lot of the same things as BitArray, but does have a method to parse binary strings - use the static BoolArray.FromBinaryString method:
BoolArray bin = BoolArray.FromBinaryString("1001011000111010101"); // etc
Here is the BoolArray source code. Note, however, that it isn't quite complete, and isn't fully tested either, but I'm not immediately aware of any bugs.
EDIT: I noticed after pasting the original link that the code used a function provided in a different class of my "Utils" library, and wouldn't have compiled directly. I've updated the link to provide this class in the code as well... hopefully that was the only case, but if not let me know and I can fix.

Categories