If you were supporting multiple formatting, you could do something like:
formatter = Formatter.new(dollar_currency)
formattedString = formatter.format(dollarAmount)
Although really if the amount is a dollar, then what about a proper
dollar class that takes the string:
dollar = Dollar.new("300,000")
Then make dollar.to_s return "$300,00". You could even have
customizable formatting then:
dollar.format(formatString);
And add currency-related methods, like conversion. This might be
overkil if it''s a one-time deal, but there is great benifit in
treating a dollar amount like a dolar amount, rather than as a string
or numeric. It''s a simple matter to map this into the DB using
ActiveRecord - see
http://api.rubyonrails.com/classes/ActiveRecord/Aggregations/ClassMethods.html
for more information on the aggregation support.
sam
On 5/10/05, Rob Park <rbpark-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
wrote:> On 5/10/05, Lucas Carlson <rails-1eRuzFDw/cg@public.gmane.org> wrote:
> > I personally put helper functions like this in app/controller/
> > application.rb
> > class Integer
> > alias _to_s to_s
> > def to_s
> > _to_s.reverse.gsub(/(\d{3})/,
''\1,'').chomp(",").reverse
> > end
> > end
>
> Keep in mind though, it''s probably not the greatest idea to
override
> to_s unless you want to do this *everywhere* that you''re printing
an
> integer.
>
> Best just to make your own custom method, and then call
> your_var.your_func when you need that formatting.
>
> --
> Urban Artography
> http://artography.ath.cx
> _______________________________________________
> Rails mailing list
> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
> http://lists.rubyonrails.org/mailman/listinfo/rails
>
--
sam
http://www.magpiebrain.com/