含有运算符的STRING得到计算结果,类似JS的EVAL
例如:STR=“8*9 —23 + 45/5”
不知道在哪看见一个人写过一个算法的,然后求得结果,不过我又看到一个人用
Dim xmlD As System.Xml.XmlDocument
Dim xmlP As System.Xml.XPath.XPathNavigator
类似前台写的EVAL就能得到结果。
后台方法计算,非常好,
写一了一个,参考:
Private Function CalculateString(ByVal str As String) As Single
Dim xmlD As System.Xml.XmlDocument
Dim xmlP As System.Xml.XPath.XPathNavigator
Try
xmlD = New System.Xml.XmlDocument
xmlP = xmlD.CreateNavigator
Return xmlP.Evaluate(str)
Catch ex As Exception
Return 0
End Try
End Function
例:STR=“8*9 —23 + 45/5”
STR=CalculateString(STR)
结果:STR=58