如何在vb.net中正确地进行简单的数学计算? [英] How to do a simple maths percetange calculation correctly in vb.net?
本文介绍了如何在vb.net中正确地进行简单的数学计算?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
可以将这个等式重新构造为格式正确的字符串吗?
Can this equation be reworked to a correctly formatted string?
Dim Total = MyNumber / 100 * MyVAT
产生错误:
"Input string was not in a correct format"
推荐答案
您应该将Option Strict
设置为on
.然后,由于MyNumber
是一个字符串(如您所述),因此永远不会编译.
You should really set Option Strict
to on
. Then this would never compile since MyNumber
is a string(as you've commented).
首先应确保输入为数字,例如使用Decimal.TryParse
:
You should first ensure that the input is numeric, for example with Decimal.TryParse
:
Dim Total As Decimal
Dim num As Decimal
If Decimal.TryParse(MyNumber, num) Then
Total = num / 100 * MyVAT
End If
现在,您可以使用Decimal.ToString(format)
将其转换为具有所需格式的字符串.例如(假设您想要两位小数):
Now you can use Decimal.ToString(format)
to convert it to a string with the desired format. For example(assuming you want two decimal places):
Dim output As String = Total.ToString("N2")
这篇关于如何在vb.net中正确地进行简单的数学计算?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文