How to do a simple maths percetange calculation correctly in vb.net?

余生颓废 提交于 2019-12-14 03:34:11

问题


Can this equation be reworked to a correctly formatted string?

Dim Total = MyNumber / 100 * MyVAT

Produces error:

"Input string was not in a correct format" 

回答1:


You should really set Option Strict to on. Then this would never compile since MyNumber is a string(as you've commented).

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

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")

Standard Numeric Format Strings




回答2:


try with this statement

 Dim Total = Val(MyNumber) / 100 * Val(MyVAT)



回答3:


use like below

Total.ToString("#")

More Detail:- http://msdn.microsoft.com/en-in/library/0c899ak8.aspx?cs-save-lang=1&cs-lang=vb#code-snippet-2



来源:https://stackoverflow.com/questions/16143839/how-to-do-a-simple-maths-percetange-calculation-correctly-in-vb-net

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!