如何使用标签文本中的公式进行计算 [英] How do I use formula from label text to calculate
问题描述
我正在创建用于计算物品重量的表格
我以字符串格式将公式存储到数据库中。
在计算的时候,我把这个公式带到我的表格中,我从用户那里获取价值。
公式是(val1-val2)* val2 * 0.00314 * val3
在用户输入后我更换了val1,val2&的值val3与用户输入
所以基本上我的公式就像(51-5)* 5 * 0.00314 * 4在这51,5,4是用户输入
我将此公式存储到标签
现在我想用这个公式来计算重量,所以我将标签值转换为双倍。
但它显示错误输入字符串的格式不正确。
任何人都可以帮助我。
我尝试过:
I was creating Form for calculating weight of item
I was stored formula to database in string format.
While calculating i was take that formula to label in my form and i am taking value from user.
the Formula is "(val1-val2)*val2*0.00314*val3"
after taking user input i was replace the value of val1, val2 & val3 with user input
so basically my formula is like "(51-5)*5*0.00314*4" in this 51,5,4 is user inputs
and i am store this formula to label
now i want to use that formula for calculating weight, so i am convert label value to double.
But it shows me error "input string is not in correct format."
Can anyone help me.
What I have tried:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim bindformula As String = lblActualFormula.Text
bindformula = bindformula.Replace("Val1", txtOd.Text)
bindformula = bindformula.Replace("Val2", txtWT.Text)
bindformula = bindformula.Replace("Val3", txtLength.Text)
lblBindFormula.Text = bindformula
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Dim val As Double = Double.Parse(lblBindFormula.Text)
MsgBox(val.ToString())
End Sub
推荐答案
您不能只尝试将表达式解析为值 - 它不能像那样工作。
你需要正确评估它 - tehre是本网站上的几位评估员:微小的表达评估器 [ ^ ]是一个很好的,您可以添加其程序集作为项目的引用,并直接从VB调用它。
You can't just try to parse an expression to a value - it doesn't work like that.
You need to evaluate it properly instead - tehre are several evaluators on this site: A Tiny Expression Evaluator[^] is a good one, and you can add its assembly as a reference to your project and call it direct from VB.
您可以使用非常快速和灵活的 .NET的计算引擎 [ ^ ]。
其他资源:
重新访问表达式评估程序(100%托管.NET中的Eval函数) [ ^ ]
使用DynamicMethod .Net Expression Evaluator [ ^ ]
如果您想了解它的工作原理,请阅读:在VB.NET中解析数学表达式:可能的任务 [ ^ ]
You can use very fast and flexible A Calculation Engine for .NET[^].
Other resources:
The expression evaluator revisited (Eval function in 100% managed .NET)[^]
.Net Expression Evaluator using DynamicMethod[^]
If you would like to understand how it works, please read: Parsing Mathematical Expressions in VB.NET: Mission Possible[^]
这篇关于如何使用标签文本中的公式进行计算的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!