问题
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