以下是我的HomeController:
def index
@products = Product.public_active
.order('random()')
.limit(6)
.includes(:product_attachments)
.includes(:product_reviews)
@products.each do |product|
if product.currency != session[:currency]
if session["currency-convert-#{product.currency}"].to_f == 0.0
rate = 1.0
else
rate = session["currency-convert-#{session[:currency]}"].to_f /
session["currency-convert-#{product.currency}"].to_f
end
else
rate = 1.0
end
product.price_with_currency = 300.00
product.current_currency = session[:currency]
end
end
这是我的观点:
<div id="product-list">
<div class="row">
<p class="price"><%= product.price_with_currency %></p>
</div>
</div>
问题是price_with_currency
输出300.0
而非300.00
的结果。为什么会这样?谢谢!
答案 0 :(得分:1)
您可以使用名为number_with_precision
它看起来像这样:
<p class="price"><%= number_with_precision(product.price_with_currency, :precision => 2) %></p>
在此处阅读更多内容:Rails 3. How to display two decimal places in edit form?
答案 1 :(得分:0)
尝试使用两者:
<p class="price"><%= number_with_precision(product.price_with_currency, precision: 2) %></p>
OR
<p class="price"><%= number_to_currency(product.price_with_currency, unit: "USD", precision: 2) %></p>
如果你想要货币:D喜欢这个USD 3,000.00
#if 3000
答案 2 :(得分:0)
您也可以使用number_to_currency帮助
例如。
<%= number_to_currency(300, :unit => "$ ", :separator => ".", :delimiter => ",") %>
## Output
$ 300.00