Module: BB::Number
- Defined in:
- lib/blackbox/number.rb
Overview
String utilities.
Constant Summary collapse
- STORAGE_UNITS =
%w(byte k M G T P E Z Y).freeze
Class Method Summary collapse
-
.to_human_size(number, options = {}) ⇒ String
Formats the bytes in
numberinto a more understandable representation (e.g., giving it 1500 yields 1.5k). -
.with_delimiter(number, options = {}) ⇒ String
Formats a
numberwith grouped thousands usingdelimiter(e.g., 12,324). -
.with_precision(number, options = {}) ⇒ String
Formats a
numberwith the specified level of:precision(e.g., 112.32 has a precision of 2).
Class Method Details
.to_human_size(number, options = {}) ⇒ String
Formats the bytes in number into a more understandable representation (e.g., giving it 1500 yields 1.5k). This method is useful for reporting file sizes to users. This method returns nil if number cannot be converted into a number. You can customize the format in the options hash.
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/blackbox/number.rb', line 46 def to_human_size(number, args = {}) begin Float(number) rescue return nil end = BB::Hash.symbolize_keys(args) precision ||= ([:precision] || 1) separator ||= ([:separator] || '.') delimiter ||= ([:delimiter] || '') kilo ||= ([:kilo] || 1024) storage_units_format ||= ([:format] || '%n%u') begin if number.to_i < kilo storage_units_format.gsub(/%n/, number.to_i.to_s).gsub(/%u/, '') else max_exp = STORAGE_UNITS.size - 1 number = Float(number) exponent = (Math.log(number) / Math.log(kilo)).to_i # Convert to base exponent = max_exp if exponent > max_exp # we need this to avoid overflow for the highest unit number /= kilo**exponent unit = STORAGE_UNITS[exponent] escaped_separator = Regexp.escape(separator) formatted_number = with_precision(number, precision: precision, separator: separator, delimiter: delimiter).sub(/(#{escaped_separator})(\d*[1-9])?0+\z/, '\1\2').sub(/#{escaped_separator}\z/, '') storage_units_format.gsub(/%n/, formatted_number).gsub(/%u/, unit) end rescue number end end |
.with_delimiter(number, options = {}) ⇒ String
Formats a number with grouped thousands using delimiter (e.g., 12,324). This method returns nil if number cannot be converted into a number. You can customize the format in the options hash.
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 |
# File 'lib/blackbox/number.rb', line 160 def with_delimiter(number, args = {}) begin Float(number) rescue return nil end = BB::Hash.symbolize_keys(args) delimiter ||= ([:delimiter] || ',') separator ||= ([:separator] || '.') begin parts = number.to_s.split('.') parts[0].gsub!(/(\d)(?=(\d\d\d)+(?!\d))/, "\\1#{delimiter}") parts.join(separator) rescue number end end |
.with_precision(number, options = {}) ⇒ String
Formats a number with the specified level of :precision (e.g., 112.32 has a precision of 2). This method returns nil if number cannot be converted into a number. You can customize the format in the options hash.
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
# File 'lib/blackbox/number.rb', line 112 def with_precision(number, args) begin Float(number) rescue return nil end = BB::Hash.symbolize_keys(args) precision ||= ([:precision] || 3) separator ||= ([:separator] || '.') delimiter ||= ([:delimiter] || '') begin rounded_number = (Float(number) * (10**precision)).round.to_f / 10**precision with_delimiter("%01.#{precision}f" % rounded_number, separator: separator, delimiter: delimiter) rescue number end end |