# Rails and decimals problem

**URL:** <https://www.ruby-forum.com/t/rails-and-decimals-problem/183767>\
**Category:** Rails\
**Created:** [February 19, 2010, 6:34pm UTC](https://www.ruby-forum.com/t/rails-and-decimals-problem/183767 "2010-02-19T18:34:58Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![fipa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/9e8a1a/32.png) [@fipa](https://www.ruby-forum.com/u/fipa)\
**Post date:** [February 19, 2010, 6:34pm UTC](https://www.ruby-forum.com/t/rails-and-decimals-problem/183767/1 "2010-02-19T18:34:58Z")

</div>

Hi,

I was working on a plugin to enhance ActiveRecord’s models so that it’s  
easy to handle decimal numbers and store them as integers. I was  
dynamically creating instance methods to output the formatted attribute  
(145 becomes 14.5)

Now there is a problem with Rails forms (for example), as they access  
the attributes with instance\_variable\_get.

So to intercept that call, should I use alias\_method\_chain?

Regards

---

<div class="post-metadata">

**Author:** ![fipa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/9e8a1a/32.png) [@fipa](https://www.ruby-forum.com/u/fipa)\
**Post date:** [February 20, 2010, 6:30pm UTC](https://www.ruby-forum.com/t/rails-and-decimals-problem/183767/2 "2010-02-20T18:30:45Z")

</div>

Ok so I’ve dropped work on the plugin and used decimal in rails  
migration to aset the column type accordingly.

But now, 130 appears as 130.0 , which is ugly and could lead people to  
misinterpret the a price as $1300. Is there an easy way to get rid of  
trailing zeros?

Regards

---

<div class="post-metadata">

**Author:** ![fipa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/9e8a1a/32.png) [@fipa](https://www.ruby-forum.com/u/fipa)\
**Post date:** [February 20, 2010, 6:39pm UTC](https://www.ruby-forum.com/t/rails-and-decimals-problem/183767/3 "2010-02-20T18:39:49Z")

</div>

There is a helper method called “number\_to\_currency” that allows you to  
set precision, unit ++

> **[ActionView::Helpers::NumberHelper](https://api.rubyonrails.org/classes/ActionView/Helpers/NumberHelper.html#M001684)**
>
> Provides methods for converting numbers into formatted strings. Methods are provided for phone numbers, currency, percentage, precision, positional notation, file size, and pretty printing.
