Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why doesn't #.# work for very small numbers?

Tags:

c#

In my program, I ran into a problem writing a small number to a context.Response that is then being read by a jQuery ajax response.

value = -0.00000015928321772662457;
context.Response.Write(value.ToString("#.#"));

returns [object XMLDocument]

However,

context.Response.Write(value.ToString("n"));

returns 0.00 as expected.

Using "n" is perfectly fine for my program, but why did "#.#" return with an XMLDocument?

like image 250
MikeKusold Avatar asked Oct 10 '22 12:10

MikeKusold


1 Answers

It displays a type name probably because something's ToString() returns null, so it instead has to display something else. You see similar behavior in Visual Studio's Locals panel:

  1. Start a new Console Application project and replace the Program class:

      class Program {
           static void Main() {
                var a = new Program( "asdf" );
                var b = new Program( "" );
                var c = new Program( null );
           } // <-- breakpoint here
    
           string _data;
           public Program( string data ) {
                _data = data;
           }
    
           public override string ToString() {
                return _data;
           }
      }
    
  2. Set a breakpoint at the closing brace (}) of Main() and run the project.

  3. Now look at the Locals panel. Visual Studio tries to call ToString() on objects for what to display between braces in the Value column on this panel. However, because one instance returns null, there is evidently a fallback of getting the type name:

    a      {asdf}
    b      {}
    c      {ConsoleApplication1.Program}
    

I figure context.Response.Write() is doing something similar. Internally it uses a TextWriter, and here's some of the process that happens in your "#.#" issue:

  1. value.ToString("#.#"), unlike value.ToString("#.0"), returns an empty string ("") because there are no non-zero digits to fill the format template (this is better than returning ".").

  2. The Write(string) method of the TextWriter calls ToCharArray(), passing that on to Write(char[]), which of course writes nothing.

The response always produces an XMLDocument to the client-side, since XML is how AJAX transports requests and responses -- even the correct "0.00" is transported in XML. Somewhere between that and your jQuery receipt of the response, something decided an empty XMLDocument should be substituted when processed. Could even be jQuery itself doing that, but I don't know the ASP.NET pipeline or jQuery enough to find where.

So evidently, empty AJAX responses are not so great.

like image 193
Joel B Fant Avatar answered Oct 13 '22 03:10

Joel B Fant