Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 339 Vote(s) - 3.45 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Rails 3. How to explicitly round a number to two decimal places in the model?

#1
> **Possible Duplicate:**
> [Rails 3. How do display two decimal places in edit form?](

[To see links please register here]

)

<!-- End of automatically inserted text -->

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

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

#2
You should try using the `:decimal` type of database field, with the `:scale` set to `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:

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

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

<% 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][1]


[1]:

[To see links please register here]

Reply

#4
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:

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

Here's [another SO question](

[To see links please register here]

) on this topic, by the way. @Matthew Rudy is right about one thing: For money columns you should be using `:decimal`.

If you're *really* committed to not having this in your view, you could use something like [Draper](

[To see links please register here]

) 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.
Reply

#5
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.
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through