概要
formatting
詳細
formatting について
formatting
数値を任意のフォーマットに変更する
フォーマット
フォーマット |
---|
phone |
currency |
percentage |
delimited |
rounded |
human_size |
human |
※実際の出力内容はサンプルコード参照
サンプル
# encoding: utf-8 require 'active_support/core_ext/numeric/conversions' require 'tbpgr_utils' I18n.enforce_available_locales = true bulk_puts_eval binding, <<-EOS 1111111111.to_s(:phone) 1111.to_s(:currency) 1111.to_s(:percentage) 1111.to_s(:delimited) 1111.2345.to_s(:rounded) 1111.2345.to_s(:rounded, precision: 2) 100.to_s(:human_size) 1024.to_s(:human_size) (1024**2).to_s(:human_size) (1024**3).to_s(:human_size) (1024**4).to_s(:human_size) 100.to_s(:human) (10**3).to_s(:human) (10**6).to_s(:human) (10**9).to_s(:human) (10**12).to_s(:human) (10**15).to_s(:human) EOS __END__ ・下記はTbpgrUtils gemの機能 bulk_puts_eval https://rubygems.org/gems/tbpgr_utils https://github.com/tbpgr/tbpgr_utils
出力
1111111111.to_s(:phone) # => "111-111-1111" 1111.to_s(:currency) # => "$1,111.00" 1111.to_s(:percentage) # => "1111.000%" 1111.to_s(:delimited) # => "1,111" 1111.2345.to_s(:rounded) # => "1111.235" 1111.2345.to_s(:rounded, precision: 2) # => "1111.23" 100.to_s(:human_size) # => "100 Bytes" 1024.to_s(:human_size) # => "1 KB" (1024**2).to_s(:human_size) # => "1 MB" (1024**3).to_s(:human_size) # => "1 GB" (1024**4).to_s(:human_size) # => "1 TB" 100.to_s(:human) # => "100" (10**3).to_s(:human) # => "1 Thousand" (10**6).to_s(:human) # => "1 Million" (10**9).to_s(:human) # => "1 Billion" (10**12).to_s(:human) # => "1 Trillion" (10**15).to_s(:human) # => "1 Quadrillion"
参照
http://guides.rubyonrails.org/active_support_core_extensions.html#formatting
https://github.com/rails/rails/blob/master/activesupport/lib/active_support/core_ext/numeric/conversions.rb
https://github.com/rails/rails/blob/master/activesupport/lib/active_support/number_helper.rb
https://github.com/rails/rails/tree/master/activesupport/lib/active_support/number_helper