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.

v13.0.2 not preserving trailing zeros when deserializing decimals with 0

See original GitHub issue

Source/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:open
  • Created 9 months ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
elgonzocommented, Feb 15, 2023

@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…

1reaction
elgonzocommented, Jan 3, 2023

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…

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 >
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 >

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