2

In database, the data type of a field "Cost" is "float" There is a record with "Cost" 300.88 After getting into DTO, the Cost becomes 300.87999988

The type of Cost variable in DTO is "double" After passing DTO to view, it displays 300.88 correctly.

When saving the record, it is required to compare with database record. The program treat it as that the value is modified. (300.88 vs 300.87999988)

Jlkha
  • 21
  • 1
  • 1
    This is an instance of the [round-off error](https://en.wikipedia.org/wiki/Round-off_error), which is why it's recommended to use [`decimal`](https://learn.microsoft.com/en-us/dotnet/api/system.decimal?view=net-5.0) values for money in C# ([source](https://stackoverflow.com/a/693376/9363973)) – MindSwipe Nov 13 '20 at 10:05
  • 1. financially relevant values should always be stored and handled as `decimal` (some DBs have the "money" type). 2. When comparing floating point numbers (`float` or `double`; **not** `decimal`) you should specify an "epsilon", i.e. a measure, how close the two values need to be to still be considered "equal". Basically `Math.Abs(value1 - value2) <= epsilon`. - For example `Math.Abs(300.87999988 - 300.88) <= 0.0001` – Corak Nov 13 '20 at 10:05
  • [What every computer scientist should know about floating-point arithmetic](https://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html#:~:text=Almost%20every%20language%20has%20a%20floating-point%20datatype%3B%20computers,must%20respond%20to%20floating-point%20exceptions%20such%20as%20overflow.) – Matthew Watson Nov 13 '20 at 10:07
  • Can I keep the database type as float? I tried change the type from double to decimal in C#. But I got following error Schema specified is not valid. Errors: Model.xxx.msl : error 2019: Member Mapping specified is not valid. The type 'Edm.Decimal[Nullable=True,DefaultValue=,Precision=,Scale=]' of member 'cost_test' in type 'xxx.yyy' is not compatible with 'SqlServer.float[Nullable=True,DefaultValue=]' of member 'cost_test' in type 'xxx.Store.yyy'. – Jlkha Nov 22 '20 at 13:50

0 Answers0