-
Notifications
You must be signed in to change notification settings - Fork 4.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Utf8JsonWriter.WriteNumberValue(double / float) doesn't allow to control decimal precision #435
Comments
It isn't possible to represent certain numbers precisely with I believe the serialiser default to generate shortest round-trippable (i.e. when you Is there a reason you want to write truncated string instead? The only reason I can think of is human readability... but JSON isn't exactly built with readability being the prime objective.
|
Thank you for the answer. Both sample numbers from my question are round-trippable, i.e. the expression (double.Parse("554.1667", CultureInfo.InvariantCulture) == 554.1667) is true. I checked the sources, currently the implementation uses "G17" format specifier. I understand the idea to minimize precision losses after serializing/deserializing floating point numbers but I'd prefer to have control on this. The reasons are simple: get more compact JSON taking into account the data (in my case) are coming from sensors which actual precision is probably 1-2 digits after decimal point. JSON is used as an interchange format when sending data to other system. |
Ah, right. it's not generating the shortest roundtrippable string, just a round-trippable string 🙂 |
What framework are you using? When I call Since floating point handling on .Net Framework is problematic and will almost certainly stay that way forever and it works well on recent .Net Core, I'm not sure it makes sense to do anything here. (In other words, I don't think it would be worth it to change the API just to make .Net Framework work better.) |
.NET Framework and .NET Core have different behaviors here. In .NET Core, the floating-point parsing/formatting APIs were updated to be IEEE compliant and to produce the shortest roundtrippable string by default: https://devblogs.microsoft.com/dotnet/floating-point-parsing-and-formatting-improvements-in-net-core-3-0/. In .NET Framework, the APIs need to remain backwards compatible and so they do not produce roundtrippable strings by default and may not parse to the nearest representable result in all scenarios. The closest we can get is to always use Not using |
2 svick: yes, I tried on .Net Framework and .NET Core 2.1, not on 3.0
2 tannergooding: thank you for explanations. Regarding my original wish, it's not something urgent of cause but at the same time will not cause any harm I think. |
Adding new API to .Net has a very high cost, so the bar for that is much higher than just "will not cause any harm". |
I don't think this is a common enough scenario to warrant built-in API capabilities. What would you do in the case of Newtonsoft.Json if you wanted to do this? One workaround here could be to provide a WriteRaw API on the Utf8JsonWriter and let the caller write whatever they like, unvalidated. Then, you could format your float/double to a string of any precision and then exclude the quotes when writing it. As it stands, this issue is not actionable. |
- Adds support for the `apple test -t=maccatalyst` command (resolves dotnet#435) - Getting system logs for the process is not working at the moment (dotnet#459) - The `apple run` command cannot detect exit code yet (dotnet#462)
I'd like to be able to write to JSON double and single numbers with specified precision, e.g. 554.1667 instead of 554.166666666667. Could not achieve this with help of Math.Round(x, <precision>): the results (the strings written to JSON) are unexpected sometimes, e.g. 554.16669999999999 instead of 554.1667 or 789.14290000000005 instead of 789.1429.
Maybe add optional "precision" parameter to Utf8JsonWriter.WriteNumberValue(double) and Utf8JsonWriter.WriteNumberValue(float) ?
The text was updated successfully, but these errors were encountered: