Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails 3. How to explicitly round a number to two decimal places in the model? [duplicate]

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.

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?

like image 518
leonel Avatar asked Oct 14 '11 19:10

leonel


1 Answers

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.

like image 156
Ankit Soni Avatar answered Oct 10 '22 04:10

Ankit Soni