Rails 3。如何在模型中明确地将一个数字四舍五入到小数点后两位?(复制)

时间:2021-05-30 11:13:12

Possible Duplicate:
Rails 3. How do display two decimal places in edit form?

可能的重复:Rails 3。如何在编辑表单中显示两个小数?

I know I can use sprintf to limit to two decimal places.

我知道我可以用sprintf来限制小数点后两位。

When I redisplay the prices in a form or just in a table, sometimes I get 80, instead of 80.00. Wouldn't be better to just store the price with the two decimal places from the very moment the price is entered in the database? That way, whenever it's displayed, it will ALWAYS show the two decimal places. If so, how?

当我以表格或表格的形式重新显示价格时,有时我会得到80,而不是80。00。将价格从输入数据库的那一刻起以小数点后两位的位置存储,不是更好吗?这样,无论何时显示,它都会显示小数点后两位。如果是这样,如何?

4 个解决方案

#1


9  

You should try using the :decimal type of database field, with the :scale set to 2

您应该尝试使用:decimal类型的数据库字段,使用:scale设置为2。

To add a decimal column to a new table;

向新表添加十进制列;

create_table :my_table do |t|
  t.decimal :my_column, :scale => 2
end

To add a column to an existing table;

向现有表添加列;

add_column :my_table, :my_column, :decimal, :scale => 2

It is wise to have precisions since some database does not have precision defaults. Such as postgresql:

由于某些数据库没有精确的默认值,所以最好使用精确值。如postgresql:

 add_column :my_table, my_column, precision: 30, scale: 2

#2


32  

You can simply use x.round(2) when displaying the number - that will always display two decimal places. It's generally recommended to store prices as integers in the backend database because they are handled better when you need a really high level of precision. In this case though, you can choose to go either way.

在显示数字时,您可以使用x.round(2)——它总是显示两个小数点。通常建议将价格存储为后端数据库中的整数,因为当您需要非常高的精度时,可以更好地处理它们。在这种情况下,你可以选择任何一种方式。

#3


20  

If it's a :float or :decimal, 80 and 80.00 in Ruby are going to be the same thing (as will 80.0000). The latter isn't "stored" with more decimal places, and trailing zeroes only make sense in the view. You can use sprintf like you said, or you can use Rails' handy number_with_precision helper:

如果它是:float或:decimal,则Ruby中的80和80.00将是相同的(80.0000也是一样的)。后者不是“存储”的小数点后面有更多的小数点,尾0只在视图中有意义。你可以像你说的那样使用sprintf,也可以使用Rails的handy number_with_precision helper:

<%= number_with_precision price, :precision => 2 %>

Here's another SO question on this topic, by the way. @Matthew Rudy is right about one thing: For money columns you should be using :decimal.

这是另一个关于这个话题的问题。@Matthew Rudy关于一件事是正确的:对于金钱专栏,你应该使用:decimal。

If you're really committed to not having this in your view, you could use something like Draper to apply decorators to your attributes (which will merely move your number_with_precision call out of the way and into a new Decorator class), but it's probably overkill in this instance.

如果您真的不想在视图中使用这个,您可以使用Draper这样的东西来对您的属性应用Decorator(这只会将您的number_with_precision调用移到一个新的Decorator类之外),但是在这个实例中,这样做可能有些过头了。

#4


7  

For this I use the number_to_currency formater. Since I am in the US the defaults work fine for me.

为此,我使用number_to_currency格式器。因为我在美国,默认设置对我来说很有效。

<% price = 45.9999 %>
<price><%= number_to_currency(price)%></price>
=> <price>$45.99</price>

You can also pass in options if the defaults don't work for you. Documentation on available options at api.rubyonrails.org

如果默认值不适合您,您也可以传入选项。关于api.rubyonrails.org上可用选项的文档

#1


9  

You should try using the :decimal type of database field, with the :scale set to 2

您应该尝试使用:decimal类型的数据库字段,使用:scale设置为2。

To add a decimal column to a new table;

向新表添加十进制列;

create_table :my_table do |t|
  t.decimal :my_column, :scale => 2
end

To add a column to an existing table;

向现有表添加列;

add_column :my_table, :my_column, :decimal, :scale => 2

It is wise to have precisions since some database does not have precision defaults. Such as postgresql:

由于某些数据库没有精确的默认值,所以最好使用精确值。如postgresql:

 add_column :my_table, my_column, precision: 30, scale: 2

#2


32  

You can simply use x.round(2) when displaying the number - that will always display two decimal places. It's generally recommended to store prices as integers in the backend database because they are handled better when you need a really high level of precision. In this case though, you can choose to go either way.

在显示数字时,您可以使用x.round(2)——它总是显示两个小数点。通常建议将价格存储为后端数据库中的整数,因为当您需要非常高的精度时,可以更好地处理它们。在这种情况下,你可以选择任何一种方式。

#3


20  

If it's a :float or :decimal, 80 and 80.00 in Ruby are going to be the same thing (as will 80.0000). The latter isn't "stored" with more decimal places, and trailing zeroes only make sense in the view. You can use sprintf like you said, or you can use Rails' handy number_with_precision helper:

如果它是:float或:decimal,则Ruby中的80和80.00将是相同的(80.0000也是一样的)。后者不是“存储”的小数点后面有更多的小数点,尾0只在视图中有意义。你可以像你说的那样使用sprintf,也可以使用Rails的handy number_with_precision helper:

<%= number_with_precision price, :precision => 2 %>

Here's another SO question on this topic, by the way. @Matthew Rudy is right about one thing: For money columns you should be using :decimal.

这是另一个关于这个话题的问题。@Matthew Rudy关于一件事是正确的:对于金钱专栏,你应该使用:decimal。

If you're really committed to not having this in your view, you could use something like Draper to apply decorators to your attributes (which will merely move your number_with_precision call out of the way and into a new Decorator class), but it's probably overkill in this instance.

如果您真的不想在视图中使用这个,您可以使用Draper这样的东西来对您的属性应用Decorator(这只会将您的number_with_precision调用移到一个新的Decorator类之外),但是在这个实例中,这样做可能有些过头了。

#4


7  

For this I use the number_to_currency formater. Since I am in the US the defaults work fine for me.

为此,我使用number_to_currency格式器。因为我在美国,默认设置对我来说很有效。

<% price = 45.9999 %>
<price><%= number_to_currency(price)%></price>
=> <price>$45.99</price>

You can also pass in options if the defaults don't work for you. Documentation on available options at api.rubyonrails.org

如果默认值不适合您,您也可以传入选项。关于api.rubyonrails.org上可用选项的文档