-1

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.

Community
  • 1
  • 1
Brandon
  • 4,491
  • 6
  • 38
  • 59

3 Answers3

1

You can't change the behavior of double.ToString() without changing the locale. You could possibly create a custom numeric format provider which formats using scientific notation; I haven't tried this to verify that it would work, but I think it's the only hope.

TypeIA
  • 16,916
  • 1
  • 38
  • 52
0

You could override culture settings for all threads in your application:

var nf = (NumberFormatInfo)Thread
    .CurrentThread
    .CurrentCulture
    .NumberFormat
    .Clone();

//setup NumberFormatInfo nf accordingly (*)

var cc = (CultureInfo)Thread.CurrentThread.CurrentCulture.Clone();
cc.NumberFormat = nf;
System.Threading.Thread.CurrentThread.CurrentCulture = cc;

This will alter behaviour for all ToString() calls.

(*) Customizing NumberFormatInfo might not be enough and as suggested by dvnrrs you will have to probably write your own format provider.

EDIT:

This unfortunately won't work. Even if somehow you would manage to switch double's format provider it will be ignored, as the documentation states:

The custom numeric format provider can be used only with the String.Format(IFormatProvider, String, Object[]) method. The other overloads of numeric formatting methods (such as ToString) that have a parameter of type IFormatProvider all pass the IFormatProvider.GetFormat implementation a Type object that represents the NumberFormatInfo type. In return, they expect the method to return a NumberFormatInfo object. If it does not, the custom numeric format provider is ignored, and the NumberFormatInfo object for the current culture is used in its place.

Community
  • 1
  • 1
BartoszKP
  • 34,786
  • 15
  • 102
  • 130
  • Have you tried to change the number of decimal places (for example)? I didn't get it to work even if i specify `myClonedNumberFormatInfo.NumberDecimalDigits = 20;` because the default format specifier used in `double.ToString` is `G` and not `N` which is needed. So it would work if you call `0.0.ToString("N");` but that OP wants to avoid. – Tim Schmelter Jan 17 '14 at 16:31
  • @TimSchmelter Yeah, I've just checked that and you're correct that this is not enough. I've added information about it in the answer. – BartoszKP Jan 17 '14 at 16:32
  • I think I will try this solution in combination with the one provided by @dvnrrs. I'm having some technical difficulties with my workstation right now but I'll let you know how I make out. – Brandon Jan 17 '14 at 16:36
  • @Brandon Yeah, I don't like this either. I'll try some other things on the weekend, and let you know if I found anything. – BartoszKP Jan 17 '14 at 16:56
0

After looking into this for the better part of a day, the only solution I was able to come up with that satisfies my original criteria was to recursively iterate the form controls and add handlers to the textbox events that would capture the given input and format the double accordingly.

Brandon
  • 4,491
  • 6
  • 38
  • 59