# Formula calculation from database

Posted by Shanpricol under Regular Expressions on 6/7/2011 | Points: 10 | Views : 4214 | 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
Posted by: Gsolvers on: 6/7/2011 [Member] Starter | Points: 25
 0 Please convert all numeric value into integer/long before doing the calculation. You can either use Convert function or Int32.parse functionBest Regards, VG www.TeacherJi.comShanpricol, if this helps please login to Mark As Answer. | Alert Moderator
Posted by: Shanpricol on: 6/8/2011 [Member] Starter | Points: 25
 0 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
 0 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
Enter a Value:

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 DeepakShanpricol, if this helps please login to Mark As Answer. | Alert Moderator
Posted by: Shanpricol on: 6/8/2011 [Member] Starter | Points: 25
 0 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