question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Deserializing decimal -> removes trailing zero

See original GitHub issue

After migrating to Json.Net 10 the behaviour of deserializing decimals has changed. In Json.Net 9 the trailing zeros also exists after deserialization. Not so in version 10. The JSON string after serialization looks identical in versions 9 and 10.

Expected behavior

I would expect that trailing zeros exists after deserialzation, e. g. 87.50

Actual behavior

After deserialization the trailing zeros doesn’t exists.

Steps to reproduce

var money1 = 87.50m;
Console.WriteLine($"Before Json-Serialization: {money1}");
var jsonString = JsonConvert.SerializeObject(money1);
Console.WriteLine($"Json-String: {jsonString}");
var money2 = JsonConvert.DeserializeObject<decimal>(jsonString);
Console.WriteLine($"After Json-Serialization: {money2}");

The console output lookes as follows:

Before Json-Serialization: 87.50 Json-String: 87.50 After Json-Serialization: 87.5

With Json.Net 9 the output looks as follows:

Before Json-Serialization: 87.50 Json-String: 87.50 After Json-Serialization: 87.50

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:12 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
TylerBrinkleycommented, Jul 6, 2017

I just created the PR #1368 to fix this.

0reactions
mvmurali5commented, Nov 24, 2020

I am having have this issue with when deserialize string to xmlDocument

XmlDocument doc = JsonConvert.DeserializeXmlNode(stringval, "Root");

Read more comments on GitHub >

github_iconTop Results From Across the Web

Preserving trailing zeros when selecting values from Json ...
Because C# decimal types preserve trailing zeros, you can just instruct Json.Net to parse numbers to decimals instead of floating points.
Read more >
JsonObject drops trailing zero from decimal type in REST ...
This type trims trailing zeros by default. It's up to the client/receiver of the JSON to decide how many decimal places it needs...
Read more >
How to remove trailing zeros in a decimal - C#
The following program explains how to remove the trailing zeros in a decimal type. Also, it is doing the rounding operation also. Example:...
Read more >
Preserving trailing zeros when selecting values from Json ...
Because C# decimal types preserve trailing zeros, you can just instruct Json.Net to parse numbers to decimals instead of floating points.
Read more >
How to configure the serialization of floats?
ArduinoJson uses as few digits as possible: it doesn't add trailing zeros. It uses up to 9 decimal places; this is a limitation...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found