rails常用helper之number

rails中 几个重要的关于number 的helper

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: ”pound”)

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, country_code: 1 # +1- 111 – 456-7890
So, based upon your requirement you can show the user phone number.

你可能感兴趣的:(rails常用helper之number)