DreamTeK
DreamTeK

Reputation: 34177

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

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" 

Upvotes: 0

Views: 16443

Answers (3)

Mandeep Singh
Mandeep Singh

Reputation: 2146

try with this statement

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

Upvotes: 2

Tim Schmelter
Tim Schmelter

Reputation: 460068

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

Upvotes: 1

शेखर
शेखर

Reputation: 17614

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

Upvotes: 0

Related Questions