If the percentage is entered by the user, then
Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click Dim pb As New PictureBox Dim foo As New gameObj(pb, gameObjType.person) Dim sInps() As String = New String() {"50.00 %", "51.00%", ".52", "53", ".54%"} For Each sampleInput As String In sInps Debug.WriteLine(ConvertPercentToDouble(sampleInput).ToString("n4")) Next End Sub Private Function ConvertPercentToDouble(s As String) As Double Dim Value As Double Dim hasPercent As Boolean = s.Contains(System.Globalization.NumberFormatInfo.CurrentInfo.PercentSymbol) Dim whereIsPC As Integer = Math.Max(s.IndexOf(" "), _ s.IndexOf(System.Globalization.NumberFormatInfo.CurrentInfo.PercentSymbol)) If Double.TryParse(s, Value) _ OrElse Double.TryParse(s.Substring(0, whereIsPC).Trim, Value) Then If Value > 1 OrElse hasPercent Then Value /= 100 Return Value Else Throw New ArgumentException("YOUR ERROR HERE") End If End Function
dbasnett
source share