Formula calculation from database

Posted by Shanpricol under Regular Expressions on 6/7/2011 | Points: 10 | Views : 4061 | Status : [Member] | Replies : 6
HI,

I have a formula in a database.
Example convert degree Celsius to Fahrenheit the formula == 5 / 9 (F - 32)
I want to convert a text-box value to Fahrenheit using the given formula.
I split the formula by each character and try to do the calculation instead it is concatenating.




Responses

Posted by: Karthikanbarasan on: 6/7/2011 [Member] [Moderator] [Microsoft_MVP] [MVP] Silver | Points: 25

Up
0
Down
Hi,

Please give some clear idea about your problem. Where you got strucked?

Thanks
Karthik
www.f5Debug.net

Shanpricol, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Gsolvers on: 6/7/2011 [Member] Starter | Points: 25

Up
0
Down
Please convert all numeric value into integer/long before doing the calculation.

You can either use Convert function or Int32.parse function

Best Regards,

VG
www.TeacherJi.com

Shanpricol, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Shanpricol on: 6/8/2011 [Member] Starter | Points: 25

Up
0
Down
Hi Karthi,

I have the formula in a string
strFormula=5 / 9 (F - 32) ;
TextBox1.Text=200

The given text box value is in Fahrenheit,I want to convert this to Celsius.
The formula is in the string as given above(I get this from the conversion table formula).
How to do the calculation.
I split each character and try to do the calculation.


Shanpricol, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Nithadeepak on: 6/8/2011 [Member] Bronze | Points: 25

Up
0
Down
Hi,
you can use NCal.First you have to download the dll from http://ncalc.codeplex.com/
Then add it as refernce in your project.

Also refer the below link.
http://ncalc.codeplex.com/wikipage?title=description&referringTitle=Home

Check out the example given below.....
aspx
<body>

<form id="form1" runat="server">
<div>
Enter a Value:
<asp:TextBox ID="txt1" runat="server" /><br />
<asp:Button ID="btn1" runat="server" onclick="btn1_Click" Text="submit" /><br />
<asp:Label runat="server" ID="lbl1" />
</div>
</form>
</body>

aspx.cs

using System;

using NCalc;
public partial class formula : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}
protected void btn1_Click(object sender, EventArgs e)
{
string formula = "5 / 9 *({0} - 32)";
string expr=String.Format(formula,txt1.Text); //replace {0} with txt1.Text;
Expression ex = new Expression(expr);
object result = ex.Evaluate();
lbl1.Text = result.ToString();
}
}


Nitha Deepak

Shanpricol, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Shanpricol on: 6/8/2011 [Member] Starter | Points: 25

Up
0
Down
Hi,

Hi I used the below code to compute the formula.It is working
string formula = "5/9*(500-32)";
DataTable dt = new DataTable();
string result = dt.Compute(formula, null).ToString();

Shanpricol, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Imagicsolution on: 9/14/2013 [Member] Starter | Points: 25

Up
0
Down
dear,

please use bellow code for your problem solution for string to calculate formula.

before it provide facility in VB Script

so now u can used SQL table Expression Datatype of column and calculate it

thanks.
Imagic Solution
www.imagicsolution.com
vijay.comp@gmail.com

class Program
{
static void Main(string[] args)
{
string formula = "(a+b)/c"; // Take it from the data base or whatever...
Console.WriteLine("Enter first number:");
string a = Console.ReadLine();
Console.WriteLine("Enter second number:");
string b = Console.ReadLine();
Console.WriteLine("Enter third number:");
string c = Console.ReadLine();

// Replace all letters with numbers
formula = formula.Replace("a", a).Replace("b", b).Replace("c", c);

// Evaluate the formula
Console.WriteLine("Formula: {0}\tResult: {1}", formula, Evaluate(formula));
Console.ReadLine();
}

public static double Evaluate(string expression)
{
DataTable table = new DataTable();
table.Columns.Add("myExpression", string.Empty.GetType(), expression);
DataRow row = table.NewRow();
table.Rows.Add(row);
return double.Parse((string)row["myExpression"]);
}

}

Mr. Vijay Movaliya
Imagic Solution
www.imagicsolution.com

Shanpricol, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response