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}