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?
It displays a type name probably because something’s
ToString()returnsnull, so it instead has to display something else. You see similar behavior in Visual Studio’s Locals panel:Start a new Console Application project and replace the
Programclass:Set a breakpoint at the closing brace (
}) ofMain()and run the project.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 returnsnull, there is evidently a fallback of getting the type name:I figure
context.Response.Write()is doing something similar. Internally it uses aTextWriter, and here’s some of the process that happens in your"#.#"issue:value.ToString("#.#"), unlikevalue.ToString("#.0"), returns an empty string ("") because there are no non-zero digits to fill the format template (this is better than returning".").The
Write(string)method of theTextWritercallsToCharArray(), passing that on toWrite(char[]), which of course writes nothing.The response always produces an
XMLDocumentto 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 emptyXMLDocumentshould 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.