SQL 价格字段的字符串、十进制或浮点数据类型?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3063968/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 06:35:21  来源:igfitidea点击:

String, decimal, or float datatype for price field?

sqlruby-on-railsdatabase

提问by never_had_a_name

I've got a price field/column (eg. 15.50 USD) and I wonder should the Rails datatype be string, decimal, or float?

我有一个价格字段/列(例如 15.50 美元),我想知道 Rails 数据类型应该是字符串、十进制还是浮点数?

回答by Tom

add_column :table, :price, :decimal, :precision => 8, :scale => 2

The code above would be your best bet.

上面的代码将是你最好的选择。

回答by Tanel Suurhans

This argument always has two sides - decimals and integers. Supporters of integers argue that decimals might not be accurate (when doing conversions) and that the BigDecimal implementation includes bugs, sometimes even segfaulting.

这个论点总是有两个方面——小数和整数。整数的支持者认为小数可能不准确(在进行转换时)并且 BigDecimal 实现包含错误,有时甚至是段错误。

For my own project, i picked up integers also, wrapped them in a custom container, converting cents to "real" amounts and back. At first it seemed nice, after a while it became really cumbersome to use - tracking when you are dealing with cents, when with formatted strings etc.

对于我自己的项目,我也选择了整数,将它们包装在一个自定义容器中,将美分转换为“真实”金额并返回。起初它看起来不错,过了一段时间它变得非常麻烦 - 当您处理美分时,使用格式化字符串等时进行跟踪。

Then i reverted to decimals - same format all the time, i can easily convert the amount to cents if needed, i get the different rounding algorithms out of the box. Im much much more satisfied with decimals.

然后我恢复为小数 - 始终使用相同的格式,如果需要,我可以轻松地将金额转换为美分,我可以立即使用不同的舍入算法。我对小数更满意。

And to address the issues about decimals not being accurate - when googling you might notice that most of the bugs are related to converting decimals into floats :) As vise already mentioned before, floats are not accurate and you should never ever convert your decimal to a float. That's the single most important thing you have to remember when dealing with decimals - you don't want to lose accuracy by conversions. Oh and i have never actually encountered any bugs with ruby 1.8.7, 1.8.7 and 1.9.1 while using BigDecimal extensively.

并解决小数不准确的问题 - 在谷歌搜索时,您可能会注意到大多数错误都与将小数转换为浮点数有关:) 正如前面已经提到的,浮点数不准确,您永远不应该将小数转换为漂浮。这是您在处理小数时必须记住的最重要的事情 - 您不想因转换而失去准确性。哦,在广泛使用 BigDecimal 时,我实际上从未遇到过 ruby​​ 1.8.7、1.8.7 和 1.9.1 的任何错误。

回答by Woot4Moo

It depends.

这取决于。

If you are performing calculations for purchase prices use a decimal.
If you are performing engineering mathematics use a float.
If you are just storing the data use a string.

如果您要计算采购价格,请使用小数。
如果您正在执行工程数学,请使用浮点数。
如果您只是存储数据,请使用字符串。

回答by vise

Floats are not accurate:

浮点数不准确:

0.3 - 0.2 - 0.1
=> -2.77555756156289e-17

Don't use them unless you only store values.

除非您只存储值,否则不要使用它们。

If you need to make calculations store the price in cents as an integer. You can easily display them as USD with a helper.

如果您需要进行计算,请将价格以美分存储为整数。您可以使用助手轻松地将它们显示为美元。

回答by Kevin Sylvestre

I recommend using integers for prices if possible. Many popular gems (such as ActiveMerchant, Money) assume the use of integers, and it is often better to store units of measurement in a base unit (such as cents).

如果可能,我建议使用整数作为价格。许多流行的 gem(例如 ActiveMerchant、Money)都假定使用整数,并且通常最好将度量单位存储在基本单位(例如美分)中。