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.
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