Help us understand the problem. What is going on with this article?

Railsのフォーマットヘルパー抜粋

More than 1 year has passed since last update.

数値 (NumberHelper)

  • 桁区切りフォーマットへ変換

    ruby
    # number_with_delimiter(number, options = {})
    number_with_delimiter 123456789 #=> "123,456,789"
    number_with_delimiter 123456789, delimiter: "-" #=> "123-456-789"
    
  • 指定の桁で丸めたフォーマットへ変換

    ruby
    # number_with_precision(number, options = {})
    number_with_precision 123456789 #=> "123456789.000"
    number_with_precision 123.456789 #=> "123.457"
    number_with_precision 123.456789, precision:4 #=> "123.4568"
    number_with_precision 123.456789, precision:4, significant: true #=> "123.5"
    
  • パーセントフォーマットへ変換

    ruby
    # number_to_percentage(number, options = {})
    number_to_percentage 1.0/4.0 * 100 #=> "25.000%"
    number_to_percentage 1.0/4.0 * 100, precision: 0  #=> "25%"
    
  • 通貨フォーマットへ変換

    ruby
    # number_to_currency(number, options = {})
    number_to_currency 123456789 #=> "$123,456,789.00"
    number_to_currency 123456789, unit: "ドル", format: "%n%u", precision: 0, delimiter: nil #=> "123456789ドル"
    "#{123456789}ドル" #=> "123456789ドル"
    
  • 単位フォーマットへ変換

    ruby
    # number_to_human(number, options = {})
    number_to_human 123456789 #=> "123 Million"
    number_to_human 123456789, units: { million: "M"}, precision: 4 #=> "123.5 M"
    
  • バイト単位フォーマットへ変換

    ruby
    # number_to_human_size(number, options = {})
    number_to_human_size 123456789 #=> "118 MB"
    number_to_human_size 1000 #=> "1000 Bytes"
    number_to_human_size 1024 #=> "1 KB"
    
  • 電話番号フォーマットへ変換

    ruby
    # number_to_phone(number, options = {})
    number_to_phone "0123456789" #=> "012-345-6789"
    number_to_phone 123456789 #=> "12-345-6789"
    number_to_phone 123456789, country_code: 81 #=> "+81-12-345-6789"
    

日時 (DateHelper)

  • 時間の差を取得

    ruby
    # distance_of_time_in_words(from_time, to_time = 0, options = {})
    distance_of_time_in_words Time.now, Time.now #=> "less than a minute"
    distance_of_time_in_words Time.now, Time.now, include_seconds: true #=> "less than 5 seconds"
    
  • 現在から指定時間までの時間の差を取得

    ruby
    # time_ago_in_words(from_time, options = {})
    time_ago_in_words(3.minutes.ago) #=> "3 minutes"
    time_ago_in_words(3.minutes.since) #=> "3 minutes"
    helper.time_ago_in_words(40.seconds.since, include_seconds: true) #=> "less than a minute"
    

テキスト (TextHelper)

  • 省略フォーマットへ変換

    ruby
    # truncate(text, options = {}, &block)
    truncate("長文は、全部表示するとえらいことになる", length: 10) #=> "長文は、全部表..."
    truncate("長文は、全部表示するとえらいことになる", length: 10, separator: "、") #=> "長文は..."
    truncate("長文は、全部表示するとえらいことになる", length: 10, separator: "、", omission: "...以下省略") #=> "長文は...以下省略"
    
  • 特定の文字を順に取得

    ruby
    # cycle(first_value, *values)
    cycle("group a", "group b", "group c") #=> "group a" 
    cycle("group a", "group b", "group c") #=> "group b"
    # current_cycle(name = "default")
    current_cycle #=> "group b"
    cycle("group a", "group b", "group c") #=> "group c"
    cycle("group a", "group b", "group c") #=> "group a" 
    # reset_cycle(name = "default")
    reset_cycle
    cycle("group a", "group b", "group c") #=> "group a" 
    
  • HTMLタグを付与したフォーマットへ変換

    ruby
    # simple_format(text, html_options = {}, options = {})
    simple_format "<blink>例</blink>" #=> "<p>例</p>"
    simple_format "<blink>例</blink>", {name: "example"}, wrapper_tag: "div", sanitize: false #=> "<div name=\"example\"><blink>例</blink></div>"
    
  • 特定文字数を元に改行整形したも文字列を取得

    ruby
    # word_wrap(text, options = {})
    word_wrap "起 承 転 結", line_width: 3 #=> "起 承\n転 結"
    word_wrap "起 承 転 結", line_width: 1 #=> "起\n承\n転\n結"
    

rails consoleで試してみる

console
helper.number_to_currency(99999999.99) #=> "$99,999,999.99"
helper.number_to_currency(99999999.99, precision: 0) #=> "$100,000,000"
# svenfuchs/rails-i18n のja.yml を適用している場合
# precision:0, unit: 円 となる
I18n.locale= :ja
helper.number_to_currency(99999999.99) #=> "100,000,000円"
helper.number_to_currency(99999999.99, precision: 2) #=> "99,999,999.99円"
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした