Json.NET decimal precision loss

后端 未结 2 1572
轮回少年
轮回少年 2020-12-21 14:16

I have an issue with deserializing decimal value.

JObject.Parse(\"{\\\"available\\\":8777.831438322572000}\")

If I type this code in VS und

相关标签:
2条回答
  • 2020-12-21 15:07

    The result of JObject.Parse("{\"available\":8777.831438322572000}") is a double. The second statement results in a decimal.

    The double has floating point precision, which is not that precise as a decimal.

    Required reading: Why Floating-Point Numbers May Lose Precision

    0 讨论(0)
  • 2020-12-21 15:14

    I find out that this problem doesn't relate to methods which take destination type as an argument. In case of untyped version method there is a setting which allows to change how json.net treats string with decimal separator. JsonReader.FloatParseHandling default value is FloatParseHandling.Double In my case the way to get correct results is:

    JObject.Load(new JsonTextReader(new StringReader(value)) { FloatParseHandling = FloatParseHandling.Decimal }, null)
    

    JsonSerializer and JsonSerializerSettings contain the same setting.

    0 讨论(0)
提交回复
热议问题