0

In my asp.net, I have double? field in my model :

public class MyModel
{
    /// <summary>
    /// VoltageCVal 
    /// </summary>
    [DataMember(Name = "voltageCVal")]
    public double? VoltageCVal { get; set; }
}

In web api ,I get the data from database and it get the result of 3.2999999523162842 before return, when it returns, I get the api result of 3.3

Why it happens?Could you help me?

Ryan
  • 19,118
  • 10
  • 37
  • 53
  • How many decimal places of precision would you like? What precision is the model defined with? Also have you compared this value to what is stored in the database? 3.3 is a very close approximation to the value, knowing that voltage is an anlogue converted value anyway, I would be inclined to accept 3.3 and move on. – Chris Schaller Mar 22 '21 at 06:18
  • @Chris Schaller The database coulmn is `float` and displays `3.29999995231628`.What strange is for some params, I get 3.3 ,sometimes I get the actual value.But before return value, they have the same `3.2999999523162842` – Ryan Mar 22 '21 at 06:21
  • 2
    This is by design, If you are seriously storing this as a float with a precision of 16 decimals and you must have them all, then have a look at this, https://stackoverflow.com/a/51705781/1690217 its the serializer you will need to work on. But seriously consider less precision, I have worked on many IoT and sensor projects, 4 decimals for voltage is usually more than enough. Your sensor will list the precision and tolerance in it's specifications, no point storing a value that is outside of that range. – Chris Schaller Mar 22 '21 at 06:36
  • Thank you very much.I just confused about why it will have two kinds of results for the same value – Ryan Mar 22 '21 at 06:59
  • It's just precision, fundamentally the value represents the _same_ number, so it's not two kinds of results at all, the serialisation process will end up rounding the values, and will trim the trailing zeros. – Chris Schaller Mar 22 '21 at 07:27

0 Answers0