Ruby-on-rails Rails 3. 如何在模型中显式地将数字四舍五入到小数点后两位?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7772436/
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
Rails 3. How to explicitly round a number to two decimal places in the model?
提问by leonel
Possible Duplicate:
Rails 3. How do display two decimal places in edit form?
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。从价格输入数据库的那一刻起,只存储两位小数的价格不是更好吗?这样,无论何时显示,它都会显示小数点后两位。如果是这样,如何?
采纳答案by Matthew Rudy
You should try using the :decimaltype of database field, with the :scaleset 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
回答by Ankit Soni
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)在显示数字时简单地使用- 这将始终显示两位小数。通常建议将价格作为整数存储在后端数据库中,因为当您需要非常高的精度时,它们会得到更好的处理。但是,在这种情况下,您可以选择任何一种方式。
回答by Jordan Running
If it's a :floator :decimal, 80and 80.00in 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 sprintflike you said, or you can use Rails' handy number_with_precisionhelper:
如果它是:float或:decimal,80并且80.00在 Ruby 中将是相同的东西(就像80.0000)。后者不会以更多小数位“存储”,并且尾随零仅在视图中有意义。你可以sprintf像你说的那样使用,或者你可以使用 Rails 的方便的number_with_precision助手:
<%= number_with_precision price, :precision => 2 %>
Here's another SO questionon this topic, by the way. @Matthew Rudy is right about one thing: For money columns you should be using :decimal.
顺便说一下,这是关于这个主题的另一个 SO 问题。@Matthew Rudy 关于一件事是正确的:对于金钱列,您应该使用:decimal.
If you're reallycommitted to not having this in your view, you could use something like Draperto apply decorators to your attributes (which will merely move your number_with_precisioncall out of the way and into a new Decorator class), but it's probably overkill in this instance.
如果您真的致力于不考虑这个,您可以使用Draper 之类的东西将装饰器应用于您的属性(这只会将您的number_with_precision调用移到新的装饰器类中),但这可能是过度的这个实例。
回答by codesponge
For this I use the number_to_currencyformater. 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>.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上可用选项的文档

