In my previous blog on Rails I discussed about some useful text helpers for rails based on my work experience on Scrumpad. Now I would like to shade on some useful number helpers in rails which made my life easy during the project work. Let’s dive into some samples!
1. number_to_currency
Software developers do the complex thing and forget about small ones. Sometimes after design discussions, writing a lot of lines of codes with complex logic create a number. Say, bill of a particular company for the month of January! When you will deliver the result to your product owner with pride, he will take a look at the number (you are expecting a pat on your back ) and ignoring your days of work may start talking with an unsatisfied face that “You forgot the currency. Is it in pound? I will be damn rich, then”. Now you come down to the earth! What a silly thing it was using number helpers in rails!! These methods are from ActionView::Helpers::NumberHelper
2. number_to_human_size
A lot of systems deal with user interaction now a days. Attachment is an important part of it. Users like to upload files like images, texts, documents and so on. Beside a beautiful icon of that attachment, it is nice to mention the size of that file. It can be easily done by rails using this function.
It can be used with precision and separator, too.
3. number_to_percentage
It formats a number as percentage strings. At some places discount is calculated dynamically based on number of active users. Here comes a useful helper.
4. number_to_phone
It formats a phone number to US phone number format. Once can customize it, too. It takes area code, country code, extension as option hash.
Let’s give some example:
So, based upon your requirement you can show the user phone number.
That’s it for today. I will come shortly with more helper that I usually use! Happy coding :-)
1. number_to_currency
Software developers do the complex thing and forget about small ones. Sometimes after design discussions, writing a lot of lines of codes with complex logic create a number. Say, bill of a particular company for the month of January! When you will deliver the result to your product owner with pride, he will take a look at the number (you are expecting a pat on your back ) and ignoring your days of work may start talking with an unsatisfied face that “You forgot the currency. Is it in pound? I will be damn rich, then”. Now you come down to the earth! What a silly thing it was using number helpers in rails!! These methods are from ActionView::Helpers::NumberHelper
number_to_currency(1234.506) # $1234.51 number_to_currency(1234.506, :precision=>3) # $1234.506 number_to_currency(1234.506, :precision=>3, :unit=>”£”) # £1234.506
2. number_to_human_size
A lot of systems deal with user interaction now a days. Attachment is an important part of it. Users like to upload files like images, texts, documents and so on. Beside a beautiful icon of that attachment, it is nice to mention the size of that file. It can be easily done by rails using this function.
number_to_human_size(123) # 123 Bytes number_to_human_size(1234) # 123 KB number_to_human_size(1234567) # 1.2 MB
It can be used with precision and separator, too.
number_to_human_size(1234567, :precision=>2, :separator=>’,’) # 1,18 MB
3. number_to_percentage
It formats a number as percentage strings. At some places discount is calculated dynamically based on number of active users. Here comes a useful helper.
number_to_percentage(100) #100%
4. number_to_phone
It formats a phone number to US phone number format. Once can customize it, too. It takes area code, country code, extension as option hash.
Let’s give some example:
number_to_phone(1115678) # 111-5678 number_to_phone(1114567890, :area_code=>true) # (111) 456-7890 number_to_phone(1114567890, :area_code=>true, :extension=>999) # (111) 456-7890 x 999 number_to_phone(1114567890, :country_code=>1 # +1- 111 - 456-7890
So, based upon your requirement you can show the user phone number.
That’s it for today. I will come shortly with more helper that I usually use! Happy coding :-)
No comments:
Post a Comment