Before anyone marks this question as a duplicate of this or this or this, please read the question as it's not quite the same.
Recently one of my users noticed a bug in my application's interface where a textbox that is supposed to contain a number was containing scientific notation instead. Obviously I've done the research and found that double.ToString()
returns scientific notation for some reason (see here).
My problem now is that I need some way, without doing the following:
public static class Extensions
{
public static string AnExtensionMethodWithADifferentName(this double dbl)
{
//Is this enough zeros? Oh well I'll just come back and code
// more if I need them...
return dbl.ToString("0.00000000000000000000000");
}
}
that will override the default ToString()
method of double.
Why don't I just do this: myDouble.ToString("0.00000");
in the one place I need it, you ask? Because it's not just one place. It's potentially thousands as this application has already been written under the assumption that ToString()
will return the proper value.
Even if I wanted to put an infinite number of zeros in some override/extension somewhere, I can't extend double
's ToString()
method because it already exists, I can't override ToString()
because I can't inherit from a sealed struct and making a wrapper for double for every property of the type in the whole application will generate one giant, unmanageable code smelly mess.
So... what do I do? I want to change ToString()
's behavior to what the function name suggests.