0

So I'm currently using the following code to parse a string into a double:

double.TryParse(resultLabel.Content.ToString(), out newNumber);

The parsing is executed but it ignores the . in the code. For Example:

  • 3.3 gets parsed to 33
  • 6.7 gets parsed to 67
  • 9.5 gets parsed to 95

Does anyone know why is that and how to fix it?

Thanks in advance!

I tried to track it down with break points where it it parses falsely and it was immediately after this line.

Edu
  • 3
  • 2
  • 2
    What locale are you in? What happens if you try to parse `3,3`, `6,7` or `9,5`? Take a look at: https://learn.microsoft.com/en-us/dotnet/api/system.double.tostring?view=net-6.0 – Flydog57 Sep 09 '22 at 15:45
  • In you current culture `.` (dot) is a hroup separator which is ignored, while `,` (comma) is decimal one. – Dmitry Bychenko Sep 09 '22 at 15:50

1 Answers1

1

You need to pass culture that use "." as decimal separator (e.g. InvariantCulture)

var resultLabel = "3.3";
var success = double.TryParse(resultLabel, out var newNumber); //success is false
success = double.TryParse(resultLabel, NumberStyles.Any, CultureInfo.InvariantCulture, out newNumber); //success is true
Andrii Khomiak
  • 565
  • 4
  • 17