在MySQL中,十进制和双精度之间是否存在任何差异(性能方面)?

时间:2021-10-18 16:35:42

In my database, I would like to store a decimal score. A score can have a value from 0 to 10, and values in between, such as 2.3 or 9.4.

在我的数据库中,我想存储小数分数。分数可以具有0到10之间的值,以及介于两者之间的值,例如2.3或9.4。

I recently learned that int only stores whole numbers and not decimals. I found out that you could use either double or decimal, but what I would like to know is if there is any difference at all?

我最近了解到int只存储整数而不是小数。我发现你可以使用双倍或小数,但我想知道的是,是否有任何差异?

I'm currently using decimal.

我目前正在使用小数。

2 个解决方案

#1


7  

Decimals are more precise, doubles are more efficient, usually.

通常,小数更精确,双倍更有效。

With decimal you can set how many digits you want to use before and after the decimal point.
Edit: You can set digit count for decimal and double, my bad.

使用小数,您可以设置要在小数点之前和之后使用的位数。编辑:您可以设置十进制和双精度的数字计数,我的不好。

Decimals are better for things like money calculations where exactitude is absolutely necessary. Doubles are better suited for your specific case where the exactitude of the score is not critical.

对于像精确度绝对必要的货币计算这样的事情,小数值更好。双打更适合您的特定情况,其中得分的准确性并不重要。

See the overview of numeric types in the MySQL documentation for more information.

有关更多信息,请参阅MySQL文档中的数字类型概述。

#2


1  

Why not use INT and let the Score go from 0 to 100? Let the application display the score divided by ten.

为什么不使用INT并让分数从0变为100?让应用程序显示得分除以10。

This rids you of the decimal places / rounding errors problem.

这可以解决小数位/舍入错误问题。

#1


7  

Decimals are more precise, doubles are more efficient, usually.

通常,小数更精确,双倍更有效。

With decimal you can set how many digits you want to use before and after the decimal point.
Edit: You can set digit count for decimal and double, my bad.

使用小数,您可以设置要在小数点之前和之后使用的位数。编辑:您可以设置十进制和双精度的数字计数,我的不好。

Decimals are better for things like money calculations where exactitude is absolutely necessary. Doubles are better suited for your specific case where the exactitude of the score is not critical.

对于像精确度绝对必要的货币计算这样的事情,小数值更好。双打更适合您的特定情况,其中得分的准确性并不重要。

See the overview of numeric types in the MySQL documentation for more information.

有关更多信息,请参阅MySQL文档中的数字类型概述。

#2


1  

Why not use INT and let the Score go from 0 to 100? Let the application display the score divided by ten.

为什么不使用INT并让分数从0变为100?让应用程序显示得分除以10。

This rids you of the decimal places / rounding errors problem.

这可以解决小数位/舍入错误问题。