.NET / C # serialization sequence issues

I have a class with a decimal property, and I'm serializing and deserializing using JSON.NET. The problem I am having is that if I say the decimal value is 100000000000023063.0, when I deserialize, it will convert to 100000000000023000. I checked JSON and definitely wrote down as 100000000000023063.0.

I looked it up &

decimal.Parse("100000000000023063.0")

=

100000000000023063.0

but

var d = (decimal)100000000000023063.0

=

100000000000023000

I can work around this problem by saving it as a string and getting a property that makes decimal.Parse (), but does anyone know why this is happening?

+5
source share
2 answers

This is because it 100000000000023063.0is a constant doublethat you then convert to decimaland accuracy is lost.

Instead, write:

var d = 100000000000023063.0M;

M #, decimal.

- .. 1M == 1.0M, decimal s.

+12

, , : i.e.

var q = 100000000000023063.0M;

M , Decimal

+1

All Articles