Hogan, Brian P.
2006-Feb-24 17:08 UTC
[Rails] Formatting values of numbers using text_field
Anyone have a quick solution on how to format a number in a text_field tag? <% text_field "price", "amount" %> returns 100.0 and it would be nice if it showed 100.00 Of course, number_with_precision works nicely, but how do you make that work with text_field? I thought maybe overriding the getter in the model. However, this means I would have to bring the helper code into the model which I''m not sure I like. Brian Hogan Web Development Learning & Technology Services Schofield 3-B University of Wisconsin-Eau Claire 715 836 3585 hoganbp@uwec.edu -------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060224/5ee27762/attachment-0001.html
Peter De Berdt
2006-Feb-26 11:34 UTC
[Rails] Formatting values of numbers using text_field
On 24 Feb 2006, at 18:08, Hogan, Brian P. wrote:> Anyone have a quick solution on how to format a number in a > text_field tag? > > <% text_field "price", "amount" %> returns 100.0 and it would be > nice if it showed 100.00 > > Of course, number_with_precision works nicely, but how do you make > that work with text_field? > > I thought maybe overriding the getter in the model. However, this > means I would have to bring the helper code into the model which > I''m not sure I like.If you mean formatting the field when the user exits the field, you should use JavaScript (or write a small JavaScript Helper method, so you can easily use it throughout your whole application) : http://www.pageresource.com/jscript/j_a_03.htm Best regards Peter De Berdt
Mark Reginald James
2006-Feb-26 11:45 UTC
[Rails] Re: Formatting values of numbers using text_field
Hogan, Brian P. wrote:> Anyone have a quick solution on how to format a number in a text_field tag? > > <% text_field "price", "amount" %> returns 100.0 and it would be nice > if it showed 100.00 > > Of course, number_with_precision works nicely, but how do you make that > work with text_field? > > I thought maybe overriding the getter in the model. However, this means > I would have to bring the helper code into the model which I''m not sure > I like.Yes, overriding the attribute getter and setter is what I do: def amount_s ''%.2f'' % amount end def amount_s= (amount) self.amount = amount.to_f end <%= text_field :price, :amount_s %> -- We develop, watch us RoR, in numbers too big to ignore.