One problem I encounter on dotnet equally in Visual Basic or C#, is when I have to parse a decimal or a double from a string. When parsing a double or a decimal, we find a mistake because in math, punctuation symbol for the decimal point, so to make a Double.Parse(number) or Decimal.Parse(number), which makes is to ignore the point and therefore take the decimal part as if it were the whole. For this does not happen, we must indicate that the number system used is the mathematician, who does not vary with culture. An example.
string number = "15.3"; double doNumber; decimal deNumber; doNumber = double.Parse(number); deNumber = decimal.Parse(number); /* Values: doNumber = 153 deNumber = 153 */ doNumber = double.Parse(number, CultureInfo.InvariantCulture); deNumber = decimal.Parse(number, CultureInfo.InvariantCulture); /* Values: doNumber = 15.3 deNumber = 15.3 */
Dim number As String = "15.3"; Dim doNumber As Double; Dim deNumber As Decimal; doNumber = double.Parse(number); deNumber = decimal.Parse(number); 'Values: 'doNumber = 153 'deNumber = 153 doNumber = double.Parse(number, CultureInfo.InvariantCulture); deNumber = decimal.Parse(number, CultureInfo.InvariantCulture); 'Values: 'doNumber = 15.3 'deNumber = 15.3
CultureInfo is a class that provides information on cultures and in particular the property InvariantCulture or CultureInfo.InvariantCulture, is the provider of culture information that does not vary by localization, see the mathematical punctuation symbols. The CultureInfo class is in the System.Globalization namespace, leaving the path as: System.Globalization.CultureInfo.InvariantCulture.