Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prefix option for number_to_human_size #327

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 6 additions & 3 deletions actionpack/lib/action_view/helpers/number_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,7 @@ def number_with_precision(number, options = {})
# * <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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where is the default specified as :binary?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Line 346. If :prefix is different than :si, the binary base will be used.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct. But then there is no :binary symbol involved here, right? I can give any value to :prefix to get the binary behavior(1024).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, because as the relative base will not be found, the default will be used.

# ==== Examples
# number_to_human_size(123) # => 123 Bytes
# number_to_human_size(1234) # => 1.21 KB
Expand Down Expand Up @@ -341,15 +342,17 @@ def number_to_human_size(number, options = {})
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)
Expand Down
11 changes: 11 additions & 0 deletions actionpack/test/template/number_helper_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,17 @@ def test_number_to_human_size
assert_equal '10 Bytes', number_to_human_size(10)
end

def test_number_to_human_size_with_si_prefix
assert_equal '3 Bytes', number_to_human_size(3.14159265, :prefix => :si)
assert_equal '123 Bytes', number_to_human_size(123.0, :prefix => :si)
assert_equal '123 Bytes', number_to_human_size(123, :prefix => :si)
assert_equal '1.23 KB', number_to_human_size(1234, :prefix => :si)
assert_equal '12.3 KB', number_to_human_size(12345, :prefix => :si)
assert_equal '1.23 MB', number_to_human_size(1234567, :prefix => :si)
assert_equal '1.23 GB', number_to_human_size(1234567890, :prefix => :si)
assert_equal '1.23 TB', number_to_human_size(1234567890123, :prefix => :si)
end

def test_number_to_human_size_with_options_hash
assert_equal '1.2 MB', number_to_human_size(1234567, :precision => 2)
assert_equal '3 Bytes', number_to_human_size(3.14159265, :precision => 4)
Expand Down