Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Hint: I was trying to come up with some code for number validation for my own learning just before you posted your question and I got this far, I would not say it is fully tested, but I did not catch bugs so far:</p> <pre><code>Class Program Private Shared Sub Main(args As String()) 'For testing, culture string nl-BE allows , as a decimal separator Dim d As Decimal = CheckNum(".", String.Empty) Console.WriteLine(d.ToString()) Console.Read() End Sub Private Shared Function CheckNum(parm_toParseStr As String, parm_cultureName As String) As Decimal 'Convert the string sent to decimal value and raise an exception if conversion falils 'Expects string to validate and culture name (e.g. en-US). If culture name not passed, current is used 'Takes care of the missing feature in try parse, namely when a string of only "." is passed, tryparse ' does not convert it to 0. Dim style As NumberStyles Dim culture As CultureInfo Dim result As Decimal If String.IsNullOrEmpty(parm_cultureName) Then parm_cultureName = Thread.CurrentThread.CurrentCulture.Name End If 'for style see: http://msdn.microsoft.com/en-us/library/system.globalization.numberstyles%28v=vs.110%29.aspx style = NumberStyles.Number Or NumberStyles.AllowLeadingSign culture = CultureInfo.CreateSpecificCulture(parm_cultureName) parm_toParseStr = parm_toParseStr.Trim() If String.IsNullOrEmpty(parm_toParseStr) Then parm_toParseStr = "0" End If ' Gets a NumberFormatInfo associated with the passed culture. Dim nfi As NumberFormatInfo = New CultureInfo(parm_cultureName, False).NumberFormat If parm_toParseStr = "+" OrElse parm_toParseStr = "-" OrElse parm_toParseStr = nfi.CurrencyDecimalSeparator Then '+ - and decimal point parm_toParseStr = "0" End If 'for tryparse see: http://msdn.microsoft.com/en-us/library/ew0seb73%28v=vs.110%29.aspx?cs-save-lang=1&amp;cs-lang=csharp#code-snippet-2 If [Decimal].TryParse(parm_toParseStr, style, culture, result) Then Return result Else Throw New ArgumentNullException("Could not convert the passed value of:{0}", parm_toParseStr) End If End Function End Class </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload