Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can you change the behavior of double.parse?

Tags:

c#

I have used double.parse a lot in my project and now found issues that other users get problems.

I should have used:

double.parse(string, CultureInfo.InvariantCulture);

everywhere.

Can I change the behavior of double.parse or do I really need to go to all places in the code and do this manually?

like image 343
nik Avatar asked Oct 30 '25 07:10

nik


2 Answers

No, you cannot override a static method.

You will have to manually refactor your code to use the correct overload of double.Parse.

like image 121
Theodoros Chatzigiannakis Avatar answered Oct 31 '25 22:10

Theodoros Chatzigiannakis


There's a possibility to override the whole application's culture by using

Application.CurrentCulture = System.Globalization.CultureInfo.InvariantCulture;

in the application's main method.

So all double.Parse(string) are now equal to double.Parse(string, CultureInfo.InvariantCulture), because it uses the invariant culture internally.

But be aware that it changes EVERYTHING that uses formating.
That means, double.ToString() also uses the invariant culture.
If this is a problem, then you have to replace every occurance of double.Parse(string)
by double.Parse(string, CultureInfo.InvariantCulture)!

like image 27
joe Avatar answered Oct 31 '25 21:10

joe



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!