# Formating Float ActiveRecord attributes in text inputs

**URL:** <https://www.ruby-forum.com/t/formating-float-activerecord-attributes-in-text-inputs/56969>\
**Category:** Rails\
**Created:** [April 6, 2006, 4:26am UTC](https://www.ruby-forum.com/t/formating-float-activerecord-attributes-in-text-inputs/56969 "2006-04-06T04:26:49Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![sserdyuk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a9a28c/32.png) [@sserdyuk](https://www.ruby-forum.com/u/sserdyuk)\
**Post date:** [April 6, 2006, 4:26am UTC](https://www.ruby-forum.com/t/formating-float-activerecord-attributes-in-text-inputs/56969/1 "2006-04-06T04:26:49Z")

</div>

I have AMOUNT field that ActiveRecord maps to Float.  
Then I use text\_field helper to generate text inputs on forms.  
Helper takes model object and attribute name and does not provide any  
formatting abilities, thus 2.40 is shown as 2.4 which is fine with  
floats but does not look good with currency.

I have worked around this issue by adding amount\_f attribute to the  
model that returns formatted string.  
I do not like this “solution” for two reasons:

1. formatting does not belong to models
2. for every Float attribute I am forced to create another read/write  
attribute with identical logic.  
So this does not look right. What else?

I have tried to override #to\_s on Float attribute but Float is a  
singleton and overriding is not allowed.  
I have tried to inherit from Float and override it there but did not get  
it to work either.

Can someone post an elegant and clean solution for this problem?

---

<div class="post-metadata">

**Author:** ![sserdyuk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a9a28c/32.png) [@sserdyuk](https://www.ruby-forum.com/u/sserdyuk)\
**Post date:** [April 6, 2006, 2:21pm UTC](https://www.ruby-forum.com/t/formating-float-activerecord-attributes-in-text-inputs/56969/2 "2006-04-06T14:21:51Z")

</div>

Sergei S. wrote:

> I have AMOUNT field that ActiveRecord maps to Float.  
> Then I use text\_field helper to generate text inputs on forms.  
> Helper takes model object and attribute name and does not provide any  
> formatting abilities, thus 2.40 is shown as 2.4 which is fine with  
> floats but does not look good with currency.

Change the line

options[“value”] ||= value\_before\_type\_cast unless field\_type == “file”

in actionpack/lib/action\_view/helpers/form\_helper.rb to

unless field\_type == “file”  
options[“value”] ||= options[“format”] ? format(options[“format”],  
value) : value\_before\_type\_cast  
end

Then you can either write:

\<%= text\_field :product, price, :format =\> ‘%.2f’ %\>

or you can write a helper:

def usd\_field(object\_name, method, options = {})  
text\_field(object\_name, method, options.merge(:format =\> ‘%.2f’))  
end

–  
We develop, watch us RoR, in numbers too big to ignore.

---

<div class="post-metadata">

**Author:** ![sserdyuk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a9a28c/32.png) [@sserdyuk](https://www.ruby-forum.com/u/sserdyuk)\
**Post date:** [April 6, 2006, 4:07pm UTC](https://www.ruby-forum.com/t/formating-float-activerecord-attributes-in-text-inputs/56969/3 "2006-04-06T16:07:35Z")

</div>

Thank you Mark,

Your helper source code quote made me realize that I can pass ‘value’  
attribute from outside, so solution is simple:

If I want to see formatted rate I  
\<%= text\_field :detail, :rate, :value =\>  
number\_to\_justcurrency(@detail.rate) %\>

And that is it! Nice. Go Rais Go!

Sergei S.  
Red Leaf Software LLC [http://www.redleafsoft.com](http://www.redleafsoft.com)

---

<div class="post-metadata">

**Author:** ![sserdyuk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a9a28c/32.png) [@sserdyuk](https://www.ruby-forum.com/u/sserdyuk)\
**Post date:** [April 6, 2006, 9:31pm UTC](https://www.ruby-forum.com/t/formating-float-activerecord-attributes-in-text-inputs/56969/4 "2006-04-06T21:31:40Z")

</div>

Sergei S. wrote:

> Your helper source code quote made me realize that I can pass ‘value’  
> attribute from outside, so solution is simple:
> 
> If I want to see formatted rate I  
> \<%= text\_field :detail, :rate, :value =\>  
> number\_to\_justcurrency(@detail.rate) %\>

That’s a good idea to avoid patching Rails by setting the value  
explicitly. You can make it general by mimicking core code:

def usd\_field(object, method, options = {})  
amount = ‘%.2f’ % instance\_variable\_get("@#{object}").send(method)  
text\_field object, method, options.merge(:value =\> amount)  
end

–  
We develop, watch us RoR, in numbers too big to ignore.
