Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 276
L-Four Avatar asked Dec 28 '11 12:12

L-Four


1 Answers

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:

  • Accuracy problem with floating numbers
  • SQL Server Float data type calculation vs decimal
like image 96
gbn Avatar answered Oct 12 '22 05:10

gbn