0

I'm trying to carry out a conversion from a string to a double. I have values like "000.2" or "000.7". I'm trying to use them for a chart. But when I use y = Convert.ToDouble(dataGridView1.Rows[i].Cells[2].Value);

and the actual value of the dataGridView is "000.7" the output will be 7????

I do not understand this.

        int rows = dataGridView1.RowCount;
        int x;
        double y;

        for (int i = 0; i < rows; i++)
        {
            x = Convert.ToInt32(dataGridView1.Rows[i].Cells[1].Value);
            y = Convert.ToDouble(dataGridView1.Rows[i].Cells[2].Value);
            this.chartHorizontal.Series["Horizontal"].Points.AddXY(x, y);
        }

Maybe someone knows what goes wrong here?

Suggested thread doesn't show me what goes wrong.

ElectricRay81
  • 121
  • 11
  • Maybe `Double.TryParse(dataGridView1.Rows[i].Cells[2].Value, out double y);` would solve the problem here? See documentation here: https://learn.microsoft.com/en-us/dotnet/api/system.double.tryparse?view=net-6.0 – Ibrennan208 Jun 30 '22 at 20:29
  • The output will be 0.7. A number cannot start at 000.111, it can start at 0.111, because it makes absolutely no sense to store more than one zero in front of – Betsq9 Jun 30 '22 at 20:32
  • Thanks for the suggestion but unfortunately not. I have also tried to adjust CultureInfo. with no good luck. – ElectricRay81 Jun 30 '22 at 20:33
  • Does this answer your question (See selected answer)? [Difference between Convert.ToDouble and double.Parse in correlation with InvariantCulture](https://stackoverflow.com/questions/46579321/difference-between-convert-todouble-and-double-parse-in-correlation-with-invaria) – Ibrennan208 Jun 30 '22 at 20:33
  • @Betsq9 Fully agree with you but it is how I get the data from a device. So the string contains 000. and a value. probably this is the issue. Maybe I need to write something to get rid of the first two zeros? – ElectricRay81 Jun 30 '22 at 20:34
  • Can you try this? ```double.TryParse(dataGridView1.Rows[i].Cells[2].Value, NumberStyles.Any, CultureInfo.InvariantCulture, out double y)``` – Betsq9 Jun 30 '22 at 20:38

1 Answers1

1

Convert.ToDouble calls Double.Parse(string s, IFormatProvider provider) internally with CultureInfo.CurrentCulture as the provider argument (see here).

It's likely that CultureInfo.CurrentCulture is causing "000.7" to be interpreted as "7" - for example, passing new CultureInfo("de") as the second parameter to Convert.ToDouble produces the same result for me that you're seeing.

To get around this, try passing CultureInfo.InvariantCulture explicitly:

y = Convert.ToDouble(dataGridView1.Rows[i].Cells[2].Value, CultureInfo.InvariantCulture);

From the documentation:

Unlike culture-sensitive data, which is subject to change by user customization or by updates to the .NET Framework or the operating system, invariant culture data is stable over time and across installed cultures and cannot be customized by users. This makes the invariant culture particularly useful for operations that require culture-independent results, such as formatting and parsing operations that persist formatted data, or sorting and ordering operations that require that data be displayed in a fixed order regardless of culture.

Donut
  • 110,061
  • 20
  • 134
  • 146