Double Value Parsing - Handling different formats vs. Try Catch

I have to compare values in my code, the first option is to convert them into double an then compare, if it returns false it will try to compare using other conditions.

These values are collected from different programs, sometimes the formats are different, like the example bellow:


If i try to convert 1.616,48 to double it will throw an error

I was trying to do something like this:

So it will try to convert and compare the values, if an error ocurrer it will return false.
Unfortunately iā€™m not familiar with expressions, so i guess there are syntax errors.

Lets sort it out step by step
For the parsing of this local number format we would do:


Detecting if we can parse or not we can check with the tryParse method
Kindly note: we imported System.Globalization to the namespaces for shortening the statements:


And with a regex we can check e.g. for which CultureInfo we can go as an alternate before parsing with a particular cultureinfo

So we can go for following cascade:

  • check with regex: [\d.,] - we do only have digits, comma and dots
  • calculate the needed CultureInfo
  • Parse with the calculated CultureInfo

with this approach we are more direct and do not derive false decissions from try catch results

1 Like

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.