I have encountered a problem in a solution that I am trying to achieve, namely I need to be able to remove trailing zeros from a double value using C#.
For instance, if I were creating two objects that represent a formula, it would be appropriate to say that the two formulas (2.0 + 7) and (2.000 + 7) are equivalent. That is, (2.0 + 7) == (2.00000 + 7). However, I'm not sure the best way to approach this is. I'm thinking regular expressions, but I'm not certain that I can get the outcome I am looking for.
It seems that there would be an issue with resolution in this case. As an example, the two formulas 2.0 + 7 and 2.000000001 + 7 aren't the same, but they are extremely close. So my question is, could a regular expression account for this? Likewise, would a regular expression be the best approach?
With more elaboration, the following should be legal.
Assert.IsTrue(new Formula("2.0 + x7").Equals(new Formula("2.000 + x7")))
Within the Formula class there is a field that stores reference to a List of strings. This list must contain the same value in order for the overridden "Equals" method to return true.
You don't need RegExp, just use Math.Round(Double, Int32).
Try this :
double num = 587.21500;
var res = Double.Parse(num.ToString("G29"));
double variable = 56.123456;
string twoplaces = String.Format("{0:0.##}", variable);
?
Related
i have two values written like this " +5.000" (first space then plus and the double value, double value is height in meters)
the first one is on textBox and second one is i'm receiving via ref key.
simply enough i want to get the result firstvalue - secondvalue = result
for example ( +5.000 - +2.800 = 2.200)
result only in digits without plus.
i have asked this question also on c# forum
https://social.msdn.microsoft.com/Forums/en-US/a024e097-8013-4771-bbf6-99c7fd4cf457/double-split-
Thanks and Best Regards
OK, I think you're trying to solve a few problems here in one question. Let's break it down.
You need to get a value from a text box. I'll assume its called txtBox in the absense of any code, so you need to write:
double a = Double.parse(txtBox.Text);
You then need to perform your calculation. This needs to be written the other way around, for example:
result = a - b;
With limited source code it's difficult to answer properly.
I was at career fair and I was asked a question "what does this query do and in which language it is".
I told that it is in .NET LINQ, but was unable to predict what it does.
Can any one help me
I wrote in .NET and tried .
var youShould = from c
in "3%.$#9/52#2%35-%#4/#./3,!#+%23 !2#526%N#/-"
select (char)(c ^ 3 << 5);
Label1.Text = youShould.ToString();
And got this output :
System.Linq.Enumerable+WhereSelectEnumerableIterator`2[System.Char,System.Char]
First of all, don't feel bad that you didn't get the answer. I know exactly what's going on here and I'd have probably just laughed and walked away if someone asked me what this did.
There's a number of things going on here, but start with the output:
var youShould = from c in "3%.$#9/52#2%35-%#4/#./3,!#+%23 !2#526%N#/-"
select (char)(c ^ 3 << 5);
Label1.Text = youShould.ToString();
>>> System.Linq.Enumerable+WhereSelectEnumerableIterator`2[System.Char,System.Char]
When you run a LINQ query, or use any of the equivalent methods like Select() that return sets, what you get back is a special, internal type of object called an "iterator", specifically, an object that implements the IEnumerable interface. .NET uses these objects all over the place; for example, the foreach loop's purpose is to iterate over iterators.
One of the most important things to know about these kind of objects is that just creating one doesn't actually "do" anything. The iterator doesn't actually contain a set of things; rather, it contains the instructions needed to produce a set of things. If you try to do something like, e.g. ToString on it, the result you get won't be very useful.
However, it does tell us one thing: it tells us that this particular iterator takes a source list of type char and returns a new set, also of type char. (I know that because I know that's what the two generic parameters of a "select iterator" do.) To get the actual results out of this iterator you just need to loop over it somehow, e.g.:
foreach (var c in youShould)
{
myLabel.Text += c;
}
or, slightly easier,
myLabel.Text = new string(youShould.ToArray());
To actually figure out what it does, you have to also recognize the second fact: LINQ treats a string as a "set of characters". It is going to process each character in that string, one at a time, and perform the bit-wise operations on the value.
The long-form equivalent of that query is something like this:
var input= "3%.$#9/52#2%35-%#4/#./3,!#+%23 !2#526%N#/-";
var output = string.Empty;
foreach (var c in input)
{
var i = (int)c;
var i2 = i ^ (3 << 5);
var c2= (char)i2;
output += c2;
}
If you did the math by hand you'd get the correct output message. To save you the brain-numbing exercise, I'll just tell you: it toggles bits 5 and 6 of the ASCII value, changing each character to one further up the ASCII table. The resulting string is:
SEND YOUR RESUME TO [an email address]
Demostrative .NET Fiddle: https://dotnetfiddle.net/x7UvYA
For each character in the string, project the character by xor-ing it with (3 left shifted by 5), then cast the numeric value back to a char.
You could generate your own code strings by running the query again over an uncoded string, because if you XOR a number twice by the same value, you'll be left with the same number you started with. (e.g. x ^ y ^ y = x)
I'll leave it as an exercise to the reader to figure out what the following is:
4()3#)3#!#$5-"#4%34
I suppose it tests:
Linq to objects
Understanding of IEnumerable<T> interface and how it relates to strings
Casting
Bitwise operations
Bitwise operator precedence
Personally, I think this is a useless test that doesn't really reflect real world problems.
I have a linq query which is not ordered the way I would like.
The Query:
return (from obj in context.table_orders
orderby obj.order_no
select obj.order_no.ToString() + '-' + obj.order_description).ToList<string>();
What happens is that my records are ordered alphabeticaly, is there a Linq keyword I can use so my records are ordered correctly (so order 30 comes before order 100)?
I want the result to be a list of string since this is used to populate a ComboBox.
Also some of the 'order_no' in the DB are like '2.10' and '9.1.1'.
What happens is that my records are ordered alphabeticaly, is there a Linq keyword I can use so my records are
ordered correctly (so order #30 comes before order #100)?
If I would get a cente everytime someone asks this I would be rich.
Yes, there is - simple answer: ORDER A NUMBER NOT A STRING.
so order #30 comes before order #100)
But #30 comes AFTER #100 for the simple reason that they ARE sorted alphabetically becase THEY ARE STRINGS.
Parse the string, convert the number to - well - a number, and order by it.
WHOEVER had the idea that order_no should be a string WITHOUT A FIXED LENGH (like 00030) should - well - ;) get a basic education on database modelling. I really like things like invoice numbers etc. to be strings (they are NOT numbers) but keeping them in (a) a defiable pattern and (b) checksummed (so that data entry errors are easily catched) should be basics ;)
This is the kind of issue you get with junior people defining databases and data models and not thinking about the consequences.
You are in for some pain - parse the string, order by parsing result.
If obj.order_no is a string, then convert it to a number for sorting
orderby Int32.Parse(obj.order_no)
or
orderby Decimal.Parse(obj.order_no)
of cause this works only if the string represents a valid number.
If order_no is not a valid number (e.g. "17.7-8A") then write a function that formats it to contain right aligned numbers, like "00017.007-0008A" and sort using this function
orderby FormatOrderNo(obj.order_no)
UPDATE
Since you are working with EF you cannot call this function in the EF part of your query. Convert the EF-result to an IEnumerable<T> and perform the sorting using LINQ-To-Objects
return (from obj in context.table_orders select ...)
.AsEnumerable()
.OrderBy(obj => FormatOrderNo(obj.order_no))
.ToList();
Based on what you said - that the numbers are not really numbers but rather custom sequence identifiers (i.e. you don't even know what level of depth you get) I would suggest implementing a custom comparer.
If you do this you can define what you exaclty want - and that is I believe something along these lines:
split the string on .
compare the sequences up to and including the array.length of the shorter sequence
if there was a shorter sequence and by now there is a tie, pick the shorter before the longer (i.e. 2.1 before 2.1.1)
if both sequences had the same length, by the end of the comparison you should know which one is 'bigger'
solved.
If you need inspiration on IComparer implementation an example below:
http://zootfroot.blogspot.co.uk/2009/09/natural-sort-compare-with-linq-orderby.html
I would either change the datatype if possible or add it as another firld if applicable.
If that is a no-go you can look at the solutions mentioned by others in this question but beware - The .ToList() option is nice for small tables if you are pulling everything from them but getting used to it will eventually give you a world of pain. You don't wanna get everything in the long run, either use a Where or top critera.
The other solutions are nice but to complex for my taste to accomplish the task.
You could go with shooting sql directly through LinqToSql. http://msdn.microsoft.com/en-us/library/bb399403.aspx
In the sql you are free to convert and sort however you like. Some think this is a great idea and some will tell you it it bad. You loose strong typing and gain performance. You will have to know WHY you take this kinds of decision, that is the most important thing imho.
Since nobody came up with a custom orderby function translatable into SQL, I went for the IComparer function like so:
public class OrderComparer<T> : IComparer<string>
{
#region IComparer<string> Members
public int Compare(string x, string y)
{
return GetOrderableValue(x.Split('-').First()).CompareTo(GetOrderableValue(y.Split('-').First()));
}
#endregion
private int GetOrderableValue(string value)
{
string[] splitValue = value.Split('.');
int orderableValue = 0;
if (splitValue.Length.Equals(1))
orderableValue = int.Parse(splitValue[0]) * 1000;
else if (splitValue.Length.Equals(2))
orderableValue = int.Parse(splitValue[0]) * 1000 + int.Parse(splitValue[1]) * 100;
else if (splitValue.Length.Equals(3))
orderableValue = int.Parse(splitValue[0]) * 1000 + int.Parse(splitValue[1]) * 100 + int.Parse(splitValue[2]) * 10;
else
orderableValue = int.Parse(splitValue[0]) * 1000 + int.Parse(splitValue[1]) * 100 + int.Parse(splitValue[2]) * 10 + int.Parse(splitValue[3]);
return orderableValue;
}
}
The values have a maximum of 4 levels.
Anyone has a recommandation?
I've found the shunting yard algorithm, but it feels like case is a little bit different.
What if the formula your entering has variables that you want to use in another formula
you enter
cout<<"enter string"<<endl;
cin>>f;
and you enter f as
3 * (X*X) + 2*X + 7
and you use that string and parse it to find the root, for example.
While it is hard to understand what you are looking for, it hints at the ability to evaluate strings as formulas. If your looking for an Eval like solution, take a look at nCalc. Stealing from their webpage:
Expression e = new Expression("2 + 3 * 5");
Debug.Assert(17 == e.Evaluate());
It also allows you to use parameters, like
e.Parameters["X"] = 10;
and then use X as part of your string to evaluate.
It let's you convert a string representing a formula into it's final value. If you are not asking about interpreting strings at formulas then I am at a loss for what you are asking. I recommend revising your question to better articulate the inputs and outputs of your proposed functionality.
One solution that comes to my mind is, may be you can create a valid C# code from that string at runtime and make it compile, and make it execute. For example your code cam become in memory like
using namespace FormulaCalculator
{
public class Calculator
{
public static object Calculate()
{
return 3 * (X*X) + 2*X + 7 ; //where X is replaced value at runtime.
}
}
}
Compile this with CodeDome in in-memory assembly and run. Here an example Example
object resultOfFormula = FormulaCalculator.Calculator.Calculate()
I know I can get whether 2 strings are equal in content, but I need to be able to get the number of characters that differ in the result of comparing 2 string values.
For instance:
"aaaBaaaCaaaDaaaEaaa"
"aaaXaaaYaaaZaaaEaaa"
so the asnwer is 3 for this case.
Is there an easy way to do this, using regex, linq or any other way?
EDIT: Also the strings are VERY long. Say 10k+ characters.
In case there are inserts and deletes:
Levenstein distance
and here's the C# implementation
You can use LINQ:
string a = "aaaBaaaCaaaDaaaEaaa";
string b = "aaaXaaaYaaaZaaaEaaa";
int result = a.Zip(b, (x, y) => x == y).Count(z => !z)
+ Math.Abs(a.Length - b.Length);
A solution with a loop is probably more efficient though.
Hey, look at this: http://en.wikipedia.org/wiki/Hamming_distance
It will help you if you want to count deletions and insertions, not only replacements.
I would simply loop over the character arrays, adding up a counter for each difference.
This will not account for strings with different lengths, however.
If both strings have the same length and do not have complicated Unicode characters like surrogates, you can loop through each character and increment a counter if the characters at that index in each string are different.
It is theoretically impossible to do it any faster. (You need to check every single character)