Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>There is no built-in way to determine the CultureInfo from a numeric string, as far as I know. And I seriously doubt it'll ever be, because there is no 100% safe way to do it.</p> <p>Until you find a better solution (eg: some change on the sender-side), I guess the best you can do is to decrease the chances of error in two steps:</p> <p><strong>1) Input data cleanup and standardization</strong>: </p> <pre><code>Dim input as String = " 99 9.000,00 " ' This way you can remove unwanted characters (anything that is not a digit, and the following symbols: ".", "-", ",") Dim fixedInput as String = Regex.Replace(input, "[^\d-,\.]", "") ' fixedInput now is "999.000,00" </code></pre> <p><strong>2) Guess yourself the format</strong>:</p> <pre><code>Dim indexOfDot as Integer = fixedInput.IndexOf(".") Dim indexOfComma as Integer = fixedInput.IndexOf(",") Dim cultureTestOrder as List(Of CultureInfo) = new List(Of CultureInfo) Dim parsingResult as Double? Try If indexOfDot &gt; 0 And indexOfComma &gt; 0 Then ' There are both the dot and the comma..let's check their order If indexOfDot &gt; indexOfComma Then ' The dot comes after the comma. It should be en-US like Culture parsingResult = Double.Parse(fixedInput, NumberStyles.Number, CultureInfo.GetCultureInfo("en-US")) Else ' The dot comes after the comma. It should be it-IT like Culture parsingResult = Double.Parse(fixedInput, NumberStyles.Number, CultureInfo.GetCultureInfo("it-IT")) End If Else If indexOfDot = fixedInput.Length-3 Then ' There is only the dot! And it is followed by exactly two digits..it should be en-US like Culture parsingResult = Double.Parse(fixedInput, NumberStyles.Number, CultureInfo.GetCultureInfo("en-US")) Else If indexOfComma = fixedInput.Length-3 Then ' There is only the comma! And it is followed by exactly two digits..it should be en-US like Culture parsingResult = Double.Parse(fixedInput, NumberStyles.Number, CultureInfo.GetCultureInfo("it-IT")) End If Catch End Try If Not parsingResult.HasValue Then Try ' There is no dot or comma, or the parsing failed for some reason. Let's try a less specific parsing. parsingResult = Double.Parse(fixedInput, NumberStyles.Any, NumberFormatInfo.InvariantInfo) Catch End Try End If If Not parsingResult.HasValue Then ' Conversion not possible, throw exception or do something else Else ' Use parsingResult.Value End If </code></pre> <p>You are not 100% safe this way, but it should be still better than your current code (and at least works as expected on the example data your provided).</p>
    singulars
    1. This table or related slice is empty.
    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.
 

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