I’m using Json.NET to output a notepad-readable JSON file. The output is nearly perfect, except very small numbers (-0.000004) are converted to scientific notation. This is not my intention.
I downloaded the sources and in JsonConvert.cs – JsonConvert.ToString(double), I see the line:
value.ToString("R", CultureInfo.InvariantCulture)
According to C# numeric formatting documentation, “R” should guarantee a round-trip, not convert to scientific notation, but it does. I changed the format string to "0.############" and it works great. But is this the right approach?
I’d say that yes, “0.############” is the right approach because it works, it’s easy to understand, and according to the documentation, the Round-Trip format may or may not use scientific notation. The only guarantee is that “a numeric value that is converted to a string will be parsed back into the same numeric value.” I see nothing about not using scientific notation.
If you want it to always be a specific number of digits, then you could use the Fixed Point format.