C # - Replacing "." to "," in double meaning
I need to read a .txt and display it. Double values ββin data are written with ".". When I use German it doesn't interpret it as a comma. Now I tried to check if the language is set to German and replace all "." from ",". The values ββare stored in an array named "_value", but that doesn't work. Here is the code:
if ((System.Threading.Thread.CurrentThread.CurrentUICulture.TwoLetterISOLanguageName) == "de")
{
for (int i = 0; i < _value.Length; i++)
{
String temp_var = Convert.ToString(_value[i]);
temp_var.Replace(".", ",");
_value[i] = Convert.ToDouble(temp_var);
}
}
source to share
Instead of checking the language, you can also specify the culture from which the conversion is performed:
// Convert string to double from the invariant culture, which treats "." as decimal:
double d = Convert.ToDouble(_value[i], CultureInfo.InvariantCulture);
// Convert double to string using the current culture, which may happen to be German and uses a ",":
string s = Convert.ToString(d);
// Or convert double to string using the specific German culture:
string s = Convert.ToString(d, new CultureInfo("de-DE"));
I don't understand that apparently the array is _value
already double[]
, so these changes must be made earlier in your code where the conversion from string to double actually occurs.
source to share
Any reason you don't just temporarily install the appropriate culture
using System.Threading;
using System.Globalization;
Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
source to share