سؤال

I am trying to read a number from the console, but if it is input via comma (ex. 2,56), it cannot be parsed successfully. It can be parsed only if it is input like this: 2.56 How can I change that? Here is a sample code:

       if (double.TryParse(stringElements[i], out doubleNum))
                {
                    averageTime.Add(doubleNum);
                }
هل كانت مفيدة؟

المحلول

replace the comma by '.' like this :

save your number in variable X , then

if (double.TryParse((X).Replace(",", "."), out tmp))
{

}

نصائح أخرى

replace with "."

Convert.ToDouble("2,56".Replace(",", "."));
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top