number to indian currency helper for rails with WebRupee

>rails has built in number_to_currency helper which takes options like unit, delimeter, seperator which displays foreign currency correctly but somehow it is not best suited for indian currency.
Below is how we managed 2 years ago to display indian currency formatted properly with comma as seperator. personally i think it could be more better than what it is currently 😉
Number to indian currency(rupees) helper
module ApplicationHelper def number_to_indian_currency(number) if number string = number.to_s.split('.') number = string[0].gsub(/(\d+)(\d{3})$/){ p = $2;"#{$1.reverse.gsub(/(\d{2})/,'\1,').reverse},#{p}"} number = number.gsub(/^,/, '') + '.' + string[1] if string[1] # remove leading comma number = number[1..-1] if number[0] == 44 end "Rs.#{number}" endSample Output for different combinations
>> helper.number_to_indian_currency(2000)=> “Rs.2,000”>> helper.number_to_indian_currency(2040)=> “Rs.2,040”>> helper.number_to_indian_currency(2040.50)=> “Rs.2,040.5”>> helper.number_to_indian_currency(2040.54)=> “Rs.2,040.54”>> helper.number_to_indian_currency(1222040.54)=> “Rs.12,22,040.54”
After doing google today found from Piyush Ranjan’s Blog that yes there are ways to optimize code.
Optimized Versionmodule ApplicationHelper def number_to_indian_currency(number) "Rs.#{number.to_s.gsub(/(\d+?)(?=(\d\d)+(\d)(?!\d))(\.\d+)?/, "\\1,")}" endendWaw one line of code, Look at the beauty of regular expression 🙂 Truely amazing !
Integrating Webrupee symbol
First include follwing stylesheet in your layout
//public/stylesheets/font.css @font-face { font-family: "WebRupee"; font-style: normal; font-weight: normal; src: local("WebRupee"), url("") format("truetype"), url("") format("woff"), url("") format("svg");}.WebRupee { font-family: 'WebRupee';}Improved Version of Helpermodule ApplicationHelper def number_to_indian_currency(number, html=true) txt = html ? content_tag(:span, 'Rs.', :class => :WebRupee) : 'Rs.' "#{txt} #{number.to_s.gsub(/(\d+?)(?=(\d\d)+(\d)(?!\d))(\.\d+)?/, "\\1,")}" endendUsage
>> helper.number_to_indian_currency(400)=> “<span class=”WebRupee”>Rs.</span> 400″>> helper.number_to_indian_currency(5921, false)=> “Rs. 5,921”>> helper.number_to_indian_currency(9921)=> “<span class=”WebRupee”>Rs.</span> 9,921″
This will show you rupees symbol on your webpages.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.