0

Following were my 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

And here is my View:

<div id="product-list">
                <div class="row">
<p class="price"><%= product.price_with_currency %></p>
</div>
</div>

The issues is, price_with_currency output the result of 300.0 not 300.00. Why it is so? Thanks!!

Aleksei Matiushkin
  • 119,336
  • 10
  • 100
  • 160
d3bug3r
  • 2,492
  • 3
  • 34
  • 74

3 Answers3

1

You could use a helper called: number_with_precision

it would look like this:

<p class="price"><%= number_with_precision(product.price_with_currency, :precision => 2) %></p>

read more here: Rails 3. How to display two decimal places in edit form?

Community
  • 1
  • 1
Albin
  • 2,912
  • 1
  • 21
  • 31
1

You can also use number_to_currency helper

For eg.

<%= number_to_currency(300, :unit => "$ ", :separator => ".", :delimiter => ",") %>

## Output
$ 300.00
Amit Sharma
  • 3,427
  • 2
  • 15
  • 20
0

Try to use either the two:

<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>

if you want currency with it :D like this USD 3,000.00 #if 3000

Jomar Gregorio
  • 171
  • 1
  • 3
  • 11