+ 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";}

2nd Oct 2017, 9:25 PM
sneeze
sneeze - avatar
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. 😉
3rd Oct 2017, 1:14 AM
Zephyr Koo
Zephyr Koo - avatar
+ 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
2nd Oct 2017, 9:52 PM
ChaoticDawg
ChaoticDawg - avatar
+ 4
Please check my updated post. 👆
3rd Oct 2017, 2:57 PM
Zephyr Koo
Zephyr Koo - avatar
+ 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.
3rd Oct 2017, 7:39 PM
sneeze
sneeze - avatar
+ 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.
3rd Oct 2017, 5:14 AM
sneeze
sneeze - avatar
0
A double can only have 1 decimal point, not 2 like your case
3rd Oct 2017, 12:53 AM
👑 Prometheus 🇸🇬
👑 Prometheus 🇸🇬 - avatar