Module: FriendlyNumbers::NumberToHumanSize
- Defined in:
- lib/friendly_numbers/number_to_human_size.rb
Constant Summary collapse
- DEFAULTS =
{ precision: 3, scales: %w(Bytes KB MB GB TB PB EB ZB YB) }
Class Method Summary collapse
-
.calculate_smallest_value_with_scale(value) ⇒ Object
:nodoc:.
-
.convert(bytes, options) ⇒ Object
:nodoc:.
-
.zero_precision_for_zero_remainder(value, default_precision = 0) ⇒ Object
:nodoc:.
Class Method Details
.calculate_smallest_value_with_scale(value) ⇒ Object
:nodoc:
19 20 21 22 23 24 25 26 |
# File 'lib/friendly_numbers/number_to_human_size.rb', line 19 def calculate_smallest_value_with_scale(value) # :nodoc: scale = 0 while value >= 1024 value /= 1024.0 scale += 1 end [value, scale] end |
.convert(bytes, options) ⇒ Object
:nodoc:
10 11 12 13 14 15 16 17 |
# File 'lib/friendly_numbers/number_to_human_size.rb', line 10 def convert(bytes, ) # :nodoc: = DEFAULTS.merge() value, scale = calculate_smallest_value_with_scale(bytes) precision = zero_precision_for_zero_remainder(value, [:precision]) format("%.#{precision}f %s", value, [:scales][scale]) end |
.zero_precision_for_zero_remainder(value, default_precision = 0) ⇒ Object
:nodoc:
28 29 30 31 32 33 34 |
# File 'lib/friendly_numbers/number_to_human_size.rb', line 28 def zero_precision_for_zero_remainder(value, default_precision = 0) # :nodoc: if (value % 1).zero? 0 else default_precision end end |