Search code examples
c#sql-servertypesentity-framework-4sqldatatypes

What's the correct data type I should use in this case?


I have to represent numbers in my database, which are amounts of chemical substances in food, like fats, energy, magnesium and others. These values are decimals in format 12345.67.

If I use decimal (5,2) as data type in SQL Server, it maps to Decimal type in Entity Framework. If I use float as data type in SQL Server, it maps to Double in Entity Framework.

I'm not sure what the best data type in SQL Server would have to be, or doesn't it really matter a lot?

EDIT - in my case it should be decimal(7,2), as mentioned in some of the remarks!

Thanks.


Solution

  • You need decimal(7,2)

    • 7 is total number of digits
    • 2 is after the decimal point

    Differences:

    • float is approximate and will give unexpected results
    • decimal is exact

    References: