+ 3
Why is 234.342.424,467 not a double
I try to convert strings into values. I have the following string. string StringF = "234.342.424,467"; I use tryparse but it returns false. It is used in a method that returns the data-type in string-format. var numberStyle = NumberStyles.AllowDecimalPoint | NumberStyles.AllowThousands; userInput = StringF; if (double.TryParse(userInput, numberStyle, CultureInfo.CurrentCulture, out double1)) {return "double";}
6 odpowiedzi
+ 7
Different cultures may use different thousand or decimal separators (i.e. dot or comma) to represent a number.
Perhaps you need to parse it with the correct culture.
📢 Update
In that case
double.Parse("234.342.424,467",
CultureInfo.GetCultureInfo("nl-NL"));
will do the job. 😉
+ 8
There is still an appropriate order to the way the separators need to be used. You should only ever have 1 decimal/dot/period in your String. Commas can be used as a separator, but they need to precede the decimal. Comma spacing doesn't seem to matter though, weirdly enough.
"234,342,424,467" // ok
"234,342,424.467" // ok
"234,342.424,467" // not ok comma used after dot/period
"234,342.424.467" // not ok multiple dots/periods used
"234,3,4242,4467" // ok
"23,4342,42,4467" // ok
"234,3,4242,4.467" // ok
+ 4
Please check my updated post. 👆
+ 2
Yes
else if (double.TryParse(userInput, numberStyle, CultureInfo.GetCultureInfo("nl-NL"), out double1)) {return "double";}
Did the job.
Good to know that you can use a specific cultural setting without the need to change it on the computer.
+ 1
I am dutch. So my culture should be nl-Nl. We use dots as thousands and a comma as decimal separator. I am not sure what the currentculture on my laptop is set to it could be en-US. I am also not sure what the culture of the playground is. Would be good to check. I have used culture.ivariant but that did not solve my problem.
Thanks, I need to test it.
0
A double can only have 1 decimal point, not 2 like your case