在这种情况下我应该使用的正确数据类型是什么?

时间:2022-11-26 16:28:34

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.

我必须在我的数据库中表示数字,这些数字是食物中的化学物质,如脂肪,能量,镁等。这些值是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.

如果我在SQL Server中使用decimal(5,2)作为数据类型,它将映射到Entity Framework中的Decimal类型。如果我在SQL Server中使用float作为数据类型,它将映射到实体框架中的Double。

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

我不确定SQL Server中最好的数据类型是什么,或者它真的不重要吗?

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

编辑 - 在我的情况下,它应该是十进制(7,2),如一些评论中提到的!

Thanks.

2 个解决方案

#1


11  

You need decimal(7,2)

你需要小数(7,2)

  • 7 is total number of digits
  • 7是总位数

  • 2 is after the decimal point
  • 2是小数点后

Differences:

  • float is approximate and will give unexpected results
  • 浮动是近似的,会产生意想不到的结果

  • decimal is exact
  • 小数是准确的

References:

#2


4  

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.

DECIMAL(7,2)会好于浮动 - 它正是你需要的(5 + 2位数)。对于浮动类型(例如浮动,双重),您可能会遇到一些问题 - 例如四舍五入。

#1


11  

You need decimal(7,2)

你需要小数(7,2)

  • 7 is total number of digits
  • 7是总位数

  • 2 is after the decimal point
  • 2是小数点后

Differences:

  • float is approximate and will give unexpected results
  • 浮动是近似的,会产生意想不到的结果

  • decimal is exact
  • 小数是准确的

References:

#2


4  

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.

DECIMAL(7,2)会好于浮动 - 它正是你需要的(5 + 2位数)。对于浮动类型(例如浮动,双重),您可能会遇到一些问题 - 例如四舍五入。