0

I have a complex object that needs to be serialized. I am using Json.Net for this purpose. But I am getting unexpected results when double datatype values are getting serialized. I have pasted a example below. Can anyone of you tell me what is going wrong in this? For example i am trying to serialize a double value 38.1439743 and the output i get is 38.143974300000004

using Newtonsoft.Json;
using System;

namespace ConsoleApp3
{
class Program
{
    static void Main(string[] args)
    {
        double inputvalue = 38.1439743;

        numericvalues n = new numericvalues();
        n.value = inputvalue;

        string serialized = JsonConvert.SerializeObject(n); // produces "38.143974300000004" <- incorrect

        Console.WriteLine(inputvalue);
        Console.WriteLine(serialized);
        Console.ReadKey();
    }

    public class numericvalues
    {
        public double value { get; set; }
    }
}
}

The Json i get after serialization is as below

{"value":38.143974300000004}

Where in i am expecting it to be

{"value":38.1439743}
Vinayak
  • 55
  • 11
  • 2
    Welcome to floating point numbers! You'd be better off using the `decimal` type if you want precision. Floating points are inherently fuzzy at high degrees of precision – DetectivePikachu Nov 21 '19 at 20:29
  • Building on @DetectivePikachu's comment, see http://www.binaryconvert.com/result_double.html?decimal=051056046049052051057055052051 – Babak Naffas Nov 21 '19 at 21:38
  • @DetectivePikachu I am trying to understand why the code behaves differently for different input, for example 38.1439741 will produce correct output but not 38.1439743 what makes these two different. – Vinayak Nov 22 '19 at 14:28
  • To understand that is to understand floating point numbers. They aren't stored as regular numbers are. A floating point number is stored with two parts, the significand which contains the numbers digits and the exponent which tells where the decimal point is. All conversions to floating point number have a level of error. Sometimes that error is very small so the numbers appear the same at your desired level of significance. Sometimes the error is large enough that you see it reflected in your result as you do with your second number. Floats are not meant to be used for precise requirements – DetectivePikachu Nov 22 '19 at 14:41
  • You need to read [What Every Programmer Should Know About Floating-Point Arithmetic](https://floating-point-gui.de/) – Brian Rogers Nov 23 '19 at 16:53

1 Answers1

1

Try chaging data type from double to decimal if that is okay in your project in order to retain the precision.

using Newtonsoft.Json;
using System;

namespace ConsoleApp3
{
class Program
{
    static void Main(string[] args)
    {
        decimal inputvalue = 38.1439743m;

        numericvalues n = new numericvalues();
        n.value = inputvalue;

        string serialized = JsonConvert.SerializeObject(n); // produces "38.143974300000004" <- incorrect

        Console.WriteLine(inputvalue);
        Console.WriteLine(serialized);
        Console.ReadKey();
    }

    public class numericvalues
    {
        public decimal value { get; set; }
    }
}
}

I am not sure on how to mark this post as duplicate and so pasting the link here. Please refer the post Json.NET decimal precision loss

sam
  • 1,937
  • 1
  • 8
  • 14
  • Changing datatype to decimal or float will work, but trying to understand what makes some numbers different. – Vinayak Nov 22 '19 at 14:32