You must Sign In to post a response.
  • Category: ASP.NET

    Calculation by passing string az parameter

    Hello,
    How to calculate "((a+b)*1000)/(c-d)*6)" by passing this string as parameter so that I can get exact output
    String is variable
    Here we can change a b c d with some integer or decimal value
    How it can b done ?
  • #763134
    Hello shoyebAzizi,

    Follow some steps which is mentioned below :

    Right click on Project -> Add Reference -> COM Objects -> Microsoft Script Control 1.0
    Add this reference in your project.

    Now Refer the below code :

    double a = 5;
    int b = 5;
    float c = 6;
    decimal d = 2;

    string expression = "((a+b)*1000) / ((c-d)*6)";
    string param = "((" + a + "+" + b + ")*1000)/((" + c + "-" + d + ")*6)";

    MSScriptControl.ScriptControl sc = new MSScriptControl.ScriptControl();
    sc.Language = "VBScript";
    object result = sc.Eval(param);

    Label lbl = new Label();
    lbl.Font.Name = "Segoe UI";
    lbl.Text = "Expression is : " + expression;
    lbl.Text += "<br />Result is : " + result.ToString();
    Page.Controls.Add(lbl);

    You can change the expression as per your requirement.

    See the attached image. It's an output i have got using above code.

    Hope this what you are looking for.

    Regards,
    Nirav Lalan
    DNS Gold Member
    "Failure is the path of least persistence"

    Delete Attachment


  • Sign In to post your comments