L-Four
L-Four

Reputation: 13551

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.

Upvotes: 5

Views: 5738

Answers (2)

psur
psur

Reputation: 4519

DECIMAL(7,2) would be better than float - it's exactly what you need (5 + 2 digits). With floating types (eg. float, double) you may have some problems - e.g. with rounding.

Upvotes: 4

gbn
gbn

Reputation: 432657

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:

Upvotes: 11

Related Questions