I work in the UK and therefore most of my currency figures will be in UK pounds. I realise that to format a number to a currency format I can use: number_to_currency(amt, :unit => "?") but it would be more convenient to set ? as the default currency symbol. What is the best way to do that? Should I edit line 39 of this file: C:\ruby\lib\ruby\gems\1.8\gems\actionpack-1.11.2\lib\action_view\helpers\number_helper.rb so that options.delete("unit") { "$" } becomes options.delete("unit") { "?" } or is there a better way? Can I set ? as the default at the web application level? An addition to the \app\helpers within the website files? I am a newbie to Ruby and Rail. (working my way through "Agile Web Development with Rails"), so more interested in best practice that quick fix. -- Posted via http://www.ruby-forum.com/.
I''ve created a helper method like this # Displays a textual representation of +number+ in british pound format (i.e.: # "?1,359.56"). def number_to_currency_gbp (number) number_to_currency(number, { :unit => "£"}) end So just call number_to_currency_gbp Hope that helps Harvey On Tuesday, February 28, 2006, at 12:33 PM, Rob Nichols wrote:>I work in the UK and therefore most of my currency figures will be in UK >pounds. I realise that to format a number to a currency format I can >use: > >number_to_currency(amt, :unit => "?") > >but it would be more convenient to set ? as the default currency symbol. >What is the best way to do that? > >Should I edit line 39 of this file: > >C:\ruby\lib\ruby\gems\1.8\gems\actionpack-1.11.2\lib\action_view\ >helpers\number_helper.rb > >so that options.delete("unit") { "$" } becomes options.delete("unit") { >"?" } > >or is there a better way? > >Can I set ? as the default at the web application level? An addition to >the \app\helpers within the website files? > >I am a newbie to Ruby and Rail. (working my way through "Agile Web >Development with Rails"), so more interested in best practice that quick >fix. > >-- >Posted via http://www.ruby-forum.com/. >_______________________________________________ >Rails mailing list >Rails@lists.rubyonrails.org >http://lists.rubyonrails.org/mailman/listinfo/rails-- Posted with http://DevLists.com. Sign up and save your time!
Thank you Harvey. I''d come up with something similar: def fmt_pounds(amt) number_to_currency(amt, :unit => "?") end Perhaps I should be happy with that, but it seems neater to me to use the built in helper, and alter the default. OK - I''ve copied the number_to_currency code from number_helper.rb to application_helper.rd in my web application. I then edited it so the code looks like this: def number_to_currency(number, options = {}) options = options.stringify_keys precision, unit, separator, delimiter = options.delete("precision") { 2 }, options.delete("unit") { "?" }, options.delete("separator") { "." }, options.delete("delimiter") { "," } separator = "" unless precision > 0 begin parts = number_with_precision(number, precision).split(''.'') unit + number_with_delimiter(parts[0], delimiter) + separator + parts[1].to_s rescue number end end Now within the application number_to_currency(10.3) produces ?10.30. Which is what I want. Is this good practice? Have I broken anything? -- Posted via http://www.ruby-forum.com/.
Rob The way I suggested seems to be the cleanest solution to me. I personally don''t like the idea of copying Rails code into your own application. I would have thought that it may cause your maintanability problems in the future. I''m a newbie to Rails myself so there maybe a neater way to alter the default but I don''t know what it is. Let me know if you find a better solution. Regards Harvey On Tuesday, February 28, 2006, at 1:06 PM, Rob Nichols wrote:>Thank you Harvey. I''d come up with something similar: > >def fmt_pounds(amt) > number_to_currency(amt, :unit => "?") >end > >Perhaps I should be happy with that, but it seems neater to me to use >the built in helper, and alter the default. > >OK - I''ve copied the number_to_currency code from number_helper.rb to >application_helper.rd in my web application. I then edited it so the >code looks like this: > >def number_to_currency(number, options = {}) > options = options.stringify_keys > precision, unit, separator, delimiter = options.delete("precision") { 2 >}, options.delete("unit") { "?" }, options.delete("separator") { "." }, >options.delete("delimiter") { "," } > separator = "" unless precision > 0 > begin > parts = number_with_precision(number, precision).split(''.'') > unit + number_with_delimiter(parts[0], delimiter) + separator + >parts[1].to_s > rescue > number > end >end > >Now within the application number_to_currency(10.3) produces ?10.30. >Which is what I want. Is this good practice? Have I broken anything? > >-- >Posted via http://www.ruby-forum.com/. >_______________________________________________ >Rails mailing list >Rails@lists.rubyonrails.org >http://lists.rubyonrails.org/mailman/listinfo/rails-- Posted with http://DevLists.com. Sign up and save your time!
Harvey Bernstein wrote:> I personally don''t like the idea of copying Rails code into your own > application.I agree. I''m sure I can adjust the way the function behaves without rewriting all the code. In a couple of places "The Programming Ruby Guide" (http://www.rubycentral.com/book/index.html) discusses changing or adding functionality for inbuilt methods and classes. However, with this example I''m not sure how to do that. I''m sure there is a neater way of doing this. Thanks again for your help -- Posted via http://www.ruby-forum.com/.