美文网首页码路拾遗
rails常用helper之number

rails常用helper之number

作者: 谢一面 | 来源:发表于2014-06-23 00:20 被阅读94次

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

    rails中 几个重要的关于number 的helper 1. number_to_currency Softwa...

  • Rails 5 Helper

    尽量避免view过于复杂,很多view中重复的代码应该放在helper中。如果他不应该属于lib或者model/c...

  • Everyday-rails-rspec - 模型测试

    简单的测试文件 测试文件model: *** 在每个测试文件的开头是:require 'rails_helper'...

  • rails 笔记(1)

    常用rails命令 1、rails new project_name: 创建一个rails 项目 2、rails ...

  • Rails中的Helper方法

    tag类的Helper form_tag 复选框 单选框 处理模型对象 Hash 数组 Hash和数组结合 for...

  • XPath Helper使用

    XPath常用插件:XPath Helper XPath Helper是一个浏览器插件,能在element中定位元...

  • ruby-Time/Date1

    鉴于前几天没提到的rails中的时间/日期常用方法,补充下面的方法。rails中常用的取时间节点的方法,对于计算天...

  • visual studio分享

    1、visual studio常用插件 Art Template Helper Auto Close Tag Au...

  • rails在测试中导入种子数据

    种子数据本来就是系统默认有的,如果有需要,可以再每个测试用例前导入。 可以再rails_helper.rb 文件内...

  • Ruby on Rails 笔记1-Rails常用命令

    CookBook-1 Rails常用命令 1. 新建程序 rails new blog 新建一个blog的rai...

网友评论

    本文标题:rails常用helper之number

    本文链接:https://www.haomeiwen.com/subject/gkrjtttx.html