liubiao4123 / servicestack

Automatically exported from code.google.com/p/servicestack
0 stars 0 forks source link

JSV float,decimal,double precision #47

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
Hello,
I'm currently encountering a small issue with the JSV serializer. It's easy to 
reproduce :
1. Serialize an object with a Float/Double/Decimal property
2. Deserialize the object.
3. Compare both object

When I compare my deserialized object with the original, the original double 
value  0.4047479892171677 is not equal to the deserialized value 
0.404747989217168.

The value has been rounded up, but I can't find any explanation in the "JSV" 
format documentation.

Original issue reported on code.google.com by Baptiste...@gmail.com on 15 Nov 2010 at 2:14

GoogleCodeExporter commented 8 years ago
The JSV format doesn't specifically round precision for floating point numbers 
itself.

Unfortunately this issue is the result of .NET loosing precision when it 
serializes the value, e.g.

double d = 0.4047479892171677;
Console.Write("Actual: 0.4047479892171677, From String: {0}", d.ToString());
//Prints
Actual: 0.4047479892171677, From String: 0.404747989217168

Original comment by demis.be...@gmail.com on 15 Nov 2010 at 2:40

GoogleCodeExporter commented 8 years ago
Thanks for the quick answer.
But it's very strange, because with a Xml Serialization or Binary serialization 
there is no loss of precision.

I assume they are not using the ToString() method to get the value.

Once again, thanks for your answer.

Original comment by Baptiste...@gmail.com on 15 Nov 2010 at 4:12