在 Ruby on Rails 中处理定点/精度小数的最佳方法

发布于 2024-11-15 12:48:33 字数 402 浏览 6 评论 0原文

因此,我正在使用 Ruby On Rails 创建一个时间跟踪应用程序,并将时间存储为代表小时的数字。

由于任何超过 0.01(36 秒)小时的时间都是无关紧要的,我只需要 2 个小数位。

我使用的是 MySQL 数据库,列类型为浮点数。虽然这在大多数情况下都有效,但时不时地我会在浮点数的计算和舍入方面遇到错误。

我对我的选择做了一些研究,发现很多人推荐使用 BigDecimal。由于我使用大量使用计算的自定义数据库查询,因此我想知道更改列类型会如何影响这一点。它是否将其存储为字符串或 yaml,或者 MySQL 本身支持它?

或者是否有等效的方法在 Ruby / Rails 中进行定点十进制算术。

我认为任何方法都需要大量重构,我怎样才能最大程度地避免这种情况?

任何见解都值得赞赏。

So I am creating a time tracking application using Ruby On Rails and am storing the time as a number representing hours.

Since anything beyond 0.01 (36 seconds ) hours is irrelevant I only need 2 decimal places.

I am using a MySQL database with a float as the column type. While this works most of the time, every now and then i get an error with the calculation and rounding of floats.

I have done some research into my options and see that a lot of people recommend using BigDecimal. Since I use a lot of custom Database querys using calculations, so I wanted to know how changing the column type would affect this. Does it store this as a string or yaml, or is it natively supported by MySQL?

Or is there an equivalent way to do fixed-point decimal arithmetic in Ruby / Rails.

I assume any method is going to require much refactoring, how can I avoid this the most?

Any insight is appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

眼眸里的那抹悲凉 2024-11-22 12:48:33

不要将时间存储为代表小时的数字,而是将其存储为代表 36 秒(或者可能是单个秒)增量的数字。

您不需要小数支持类型来执行定点,只需除以业务逻辑即可获取小时数。

Instead of storing the time as a number representing hours, store it as a number representing increments of 36 seconds (or maybe individual seconds).

You shouldn't need a decimal supporting type to do fixed-point, simply divide in the business logic to get hours.

满身野味 2024-11-22 12:48:33

MySQL 确实有内置的 BigDecimal 支持。 http://dev.mysql.com/ doc/refman/5.1/en/ precision-math-decimal-changes.html

我建议使用它;它在我的 Rails 应用程序中运行良好。允许数据库而不是应用程序来处理这个问题会让生活变得更轻松 - 您按照抽象的设计方式使用它们。

以下是迁移代码:

change_column :table_name, :column_name, :decimal

参考:Rails 迁移更改列

MySQL does have built-in BigDecimal support. http://dev.mysql.com/doc/refman/5.1/en/precision-math-decimal-changes.html

I would suggest using that; it works well in my Rails applications. Allowing the database to handle that instead of the application makes life easier - you're using the abstractions the way they're designed.

Here's the migration code:

change_column :table_name, :column_name, :decimal

Reference: Rails migration for change column

神爱温柔 2024-11-22 12:48:33

我们实际上构建了一个时间跟踪应用程序(http://www.yanomo.com)并将我们所有的时间存储为它们代表使用 MySQL 作为底层 dbms 的小时数。对于列类型,我们使用 DECIMAL( precision,scale)。在你的情况下,像 DECIMAL(5,2) 这样的东西就可以了。在我们的业务逻辑(JAVA)中,我们使用 BigDecimal。

We have actually build a time tracking app (http://www.yanomo.com) and store all our times as the number of hours they represent with MySQL as the underlying dbms. For the column type we use DECIMAL(precision,scale). In your case something like DECIMAL(5,2) would do. In our businesslogic (JAVA) we use BigDecimal.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文