Click here to Skip to main content
15,867,704 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi all,

I am working on Xml with winforms.

here my formula is dynamic(i.e admin can change this at any time. user's can't see this)
my formula is
//((((A*12)*C)*B)*0.000578703704 )/1.2
//here Abc are static remaining are dynamic any time he can chage.


here what I am doing is when calculation we can get the formula and replace the abc into Integer values that is come from textboxes.
finally I got a result this
//((((8*12)*0)*0)*0.000578703704 )/1.2
//here I need convert to double how can I do this?


my code is
private int Result()
        {
            int i = 0;
            try
            {
                if (string.IsNullOrEmpty(txtLength.Text))
                    txtLength.Text = "0";
                if (string.IsNullOrEmpty(txtWidth.Text))
                    txtWidth.Text = "0";
                if (string.IsNullOrEmpty(txtThick.Text))
                    txtThick.Text = "0";
                double A = txtLength.Text == "" ? 0 : Convert.ToDouble(txtLength.Text);
                double B = txtWidth.Text == "" ? 0 : Convert.ToDouble(txtWidth.Text);
                double C = txtThick.Text == "" ? 0 : Convert.ToDouble(txtThick.Text);
                if (File.Exists(path))
                {
                    XDocument xDoc = XDocument.Load(path);
                    var formulas = from person in xDoc.Descendants("Formulas")
                                   select new
                                   {
                                       Formula = person.Element("Test").Value,
                                   };
                    foreach (var formula in formulas)
                    {
                        Formula = formula.Formula;
                    }
                }
                
                 //formula
                 //((((A*12)*C)*B)*0.000578703704 )/1.2
                string str = Formula.Replace("A", Convert.ToString(A));
                string str1 = str.Replace("C", Convert.ToString(C));
                var str2 = str1.Replace("B", Convert.ToString(B));
                //after replace result=((((8*12)*0)*0)*0.000578703704 )/1.2//(this is convert to double)
                double d = Convert.ToDouble(str2);
                i = Convert.ToInt32(Math.Ceiling(d));
                            }
            catch (Exception ex)
            {
            }
            return i;
        }


how can I do this?
Posted
Updated 4-Apr-13 0:07am
v4
Comments
Aarti Meswania 4-Apr-13 6:55am    
you want to evaluate equation?
((((8*12)*0)*0)*0.000578703704 )/1.2 ???
U@007 4-Apr-13 7:07am    
this is my answer. I want to convert to double?
Aarti Meswania 4-Apr-13 7:10am    
then you can't because brackets like (){}[]... and special chars like */+-... are not degits
U@007 4-Apr-13 7:24am    
So what will do?
Aarti Meswania 4-Apr-13 7:26am    
((((8*12)*0)*0)*0.000578703704 )/1.2 this can not converted in double
but result of ((((8*12)*0)*0)*0.000578703704 )/1.2 = 0 so the result can be converted

Hi everyone,

Why isn't anyone telling him the real deal directly? Is it because, it's too obvious? *Shake Head*

Aarti: Good attempt though :)

U@007: The only string you can convert to a double is a direct number such as 1.2, 5.6, 7.8, etc. What you are trying to convert to a double is not a simple string, it is an 'expression'. You cannot directly convert, such an expression into a double. You need to write a class to evaluate the expression and returns the result as a double. Does it make sense? Is this what you are trying to achieve? If so, try reading the below link and addressing your problem in that aspect.

Runtime C# Expression Evaluator[^]
http://csharpeval.codeplex.com/[^]
Evaluate C# Code (Eval Function)[^]

Hope this helps, Regards
 
Share this answer
 
Comments
U@007 5-Apr-13 2:48am    
thank you Hawkz.
:)
5+
C#
if(Double.TryParse(str2) d = Double.Parse(str2);


That should work for you.
 
Share this answer
 
Comments
U@007 4-Apr-13 6:24am    
r u tryied this?
Following should Help You.....

double d = Convert.ToDouble(Val(str2));
 
Share this answer
 
Comments
U@007 4-Apr-13 6:24am    
what is the Val? here
[no name] 4-Apr-13 7:05am    
public static Double Val(string value)
{
String result = String.Empty;
foreach (char c in value)
{
if (Char.IsNumber(c) || (c.Equals('.') && result.Count(x => x.Equals('.')) == 0))
result += c;
else if (!c.Equals(' '))
return String.IsNullOrEmpty(result) ? 0 : Convert.ToDouble(result);
}
return String.IsNullOrEmpty(result) ? 0 : Convert.ToDouble(result);
}
U@007 4-Apr-13 7:26am    
this is not working?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900