0

I have an json message as shown below

 static string s = @"{
'SampleNumber': '1605',
'Observations': [
    {
        'Value': 170.116262869025,
        'UpperManufacturerRange': 174.00783830400004,
        'LowerManufacturerRange': 166.00783830400004,
        'UpperControlRangeFromDevice': 174.00783830400003,
        'LowerControlRangeFromDevice': 166.00783830400003
    }
],
'LastUpdatedUtcTime': '2018-10-30T11:39:19.784942+00:00'
}";

This is my console application https://dotnetfiddle.net/nW7oqj

When I check the values in selectedJObject numeric values are changed!

174.00783830400004 changed to 174.00783830400005

xdtTransform
  • 1,986
  • 14
  • 34
kudlatiger
  • 3,028
  • 8
  • 48
  • 98

1 Answers1

1

This is due to floating point math.

Try to use the following class to convert, the decimal property ensures that accuracy for numbers is maintained.

public class Test
{
    public string SampleNumber;
    public string Status;
    public int QualityControlType;
    public object[] CustomFields;
    public Test1[] Observations;
}

public class Test1
{
    public object Parameter;
    public decimal UpperManufacturerRange;
}

public static void Main()
{
         var result = Newtonsoft.Json.JsonConvert.DeserializeObject<Test>(s);            
         Console.Write(result.Observations[0].UpperManufacturerRange);
}

You will see now the numbers is same, i.e., precision is accurately maintained.

peeyush singh
  • 1,337
  • 1
  • 12
  • 23