diff options
author | José Valim <jose.valim@gmail.com> | 2011-04-28 05:38:46 -0700 |
---|---|---|
committer | José Valim <jose.valim@gmail.com> | 2011-04-28 05:38:46 -0700 |
commit | bf50222b0b5417af554a9a4a8d277f4fccea8f8c (patch) | |
tree | 1d1b29a12fc890f6becc1a160de4d9f23b27c207 /actionpack/lib/action_view/helpers/number_helper.rb | |
parent | 533efd891a9f3d747e8a98e6730e56307e1c36b9 (diff) | |
parent | 07bbaaa3b14d7048a3e43851aed199b8b515a820 (diff) | |
download | rails-bf50222b0b5417af554a9a4a8d277f4fccea8f8c.tar.gz rails-bf50222b0b5417af554a9a4a8d277f4fccea8f8c.tar.bz2 rails-bf50222b0b5417af554a9a4a8d277f4fccea8f8c.zip |
Merged pull request #327 from dcrec1/master.
Prefix option for number_to_human_size
Diffstat (limited to 'actionpack/lib/action_view/helpers/number_helper.rb')
-rw-r--r-- | actionpack/lib/action_view/helpers/number_helper.rb | 9 |
1 files changed, 6 insertions, 3 deletions
diff --git a/actionpack/lib/action_view/helpers/number_helper.rb b/actionpack/lib/action_view/helpers/number_helper.rb index b545031fcc..63d13a0f0b 100644 --- a/actionpack/lib/action_view/helpers/number_helper.rb +++ b/actionpack/lib/action_view/helpers/number_helper.rb @@ -304,6 +304,7 @@ module ActionView # * <tt>:separator</tt> - Sets the separator between the fractional and integer digits (defaults to "."). # * <tt>:delimiter</tt> - Sets the thousands delimiter (defaults to ""). # * <tt>:strip_insignificant_zeros</tt> - If +true+ removes insignificant zeros after the decimal separator (defaults to +true+) + # * <tt>:prefix</tt> - If +:si+ formats the number using the SI prefix (defaults to :binary) # ==== Examples # number_to_human_size(123) # => 123 Bytes # number_to_human_size(1234) # => 1.21 KB @@ -341,15 +342,17 @@ module ActionView options[:strip_insignificant_zeros] = true if not options.key?(:strip_insignificant_zeros) storage_units_format = I18n.translate(:'number.human.storage_units.format', :locale => options[:locale], :raise => true) + + base = options[:prefix] == :si ? 1000 : 1024 - if number.to_i < 1024 + if number.to_i < base unit = I18n.translate(:'number.human.storage_units.units.byte', :locale => options[:locale], :count => number.to_i, :raise => true) storage_units_format.gsub(/%n/, number.to_i.to_s).gsub(/%u/, unit).html_safe else max_exp = STORAGE_UNITS.size - 1 - exponent = (Math.log(number) / Math.log(1024)).to_i # Convert to base 1024 + exponent = (Math.log(number) / Math.log(base)).to_i # Convert to base exponent = max_exp if exponent > max_exp # we need this to avoid overflow for the highest unit - number /= 1024 ** exponent + number /= base ** exponent unit_key = STORAGE_UNITS[exponent] unit = I18n.translate(:"number.human.storage_units.units.#{unit_key}", :locale => options[:locale], :count => number, :raise => true) |