How to correctly use CultureInfo.InvariantCulture
I'm trying to read a number from a user input (string) like:
' Where "." is grouping separator and "," is the decimal character
dim strUserInput as strin开发者_如何学Gog = "172.500,00"
dim ret as double
' Produces 172.5 ("." is the decimal separator)
ret = val(strUserInput)
'
' Alternative Way
' Still producing 172.5
strUserInput = strUserInput.tostring(CultureInfo.InvariantCulture) 'nothing changes
ret = val(strUserInput)
How can I correctly use CultureInfo to return 172,500.00 (or 172500,00 or just 172500) to a double var?
Double.Parse("175.500,00", CultureInfo.GetCultureInfo("whatever culture the input is"))
The German culture "de" uses decimal comma and period as thousand separator, so you can use that if you don't know.
From the MSDN documentation for val:
The Val function recognizes only the period (.) as a valid decimal separator. When different decimal separators are used, as in international applications, use CDbl or CInt instead to convert a string to a number. To convert the string representation of a number in a particular culture to a numeric value, use the numeric type's Parse(String, IFormatProvider) method. For example, use System.Double.Parse(System.String,System.IFormatProvider) when converting a string to a Double.
精彩评论