v13.0.2 not preserving trailing zeros when deserializing decimals with 0
See original GitHub issueSource/destination types
public sealed class Invoice
{
public Guid Id { get; set; }
public string? InvoiceNumber { get; set; }
public List<InvoiceLine>? InvoiceLines { get; set; }
}
public sealed class InvoiceLine
{
public Guid Id { get; set; }
public decimal VatAmount { get; set; }
public decimal NetAmount { get; set; }
public decimal TotalAmount { get; set; }
}
Source/destination JSON
{
"id": "d28888e9-2ba9-473a-a40f-e38cb54f9c25",
"invoiceLines": [
{
"id": "b1bc25c7-0a67-405e-b82a-b39f63d6c1ad",
"vatAmount": 0.00,
"netAmount": 90.00,
"totalAmount": 100.00
},
{
"d": "3cdce072-1824-4c9e-8bfd-c9fd4e739f5a",
"vatAmount": 15.00,
"netAmount": 85.00,
"totalAmount": 100.00
}
]
}
Expected behavior
line1-> “vatAmount”: 0.00, should have been serialized as “0.00”
Actual behavior
line1-> “vatAmount”: 0.00, have been serialized as “0”
Steps to reproduce
public class TrailingZerosTests
{
[Fact]
public void DeserializeObject_WhenSomeZerosOnADecimalValue_ShouldPreserveTrailingZeros()
{
var invoiceJson = File.ReadAllText("invoice.json");
var invoice = JsonConvert.DeserializeObject<Invoice>(invoiceJson);
invoice.Should().NotBeNull();
invoice?.InvoiceLines?[0].VatAmount.ToString(CultureInfo.InvariantCulture).Should().Be("0.00");
}
}
Issue Analytics
- State:
- Created 9 months ago
- Comments:5 (1 by maintainers)
Top 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 >Decimal in rust_decimal - Rust
Zeros between non-zero digits are always significant. Leading zeros are never significant. Trailing zeros are only significant if the number contains a decimal...
Read more >IHow to force display of trailing zeros for fixed point numbers?
I set the display format to Floating point, with 1 digit of precision. The "Hide trailing zeros" checkbox is NOT checked. What am...
Read more >Keeping a zero after a decimal - MATLAB Answers
Hi! I'm relatively new to MATLAB and am trying to round the values within this column vector to three significant digits. This has...
Read more >Leading zero
A leading zero is any 0 digit that comes before the first nonzero digit in a number string ... Therefore, the usual decimal...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@Alperen2558 i can’t tell because i am not associated nor involved in any way with the Newtonsoft.Json project. That’s a question only the author of Newtonsoft.Json can give an answer to…
Duplicate of https://github.com/JamesNK/Newtonsoft.Json/issues/2768.
The author of Newtonsoft.Json has already committed a fix to the repository, and should likely land with the next Newtonsoft.Json version…