Class: Numeric
- Defined in:
- lib/nutella/core_ext/numeric/bytes.rb,
lib/nutella/core_ext/numeric/format.rb,
lib/nutella/core_ext/numeric/percent.rb,
lib/nutella/core_ext/numeric/sanity_check.rb
Constant Summary collapse
- KILOBYTE =
1024
- MEGABYTE =
KILOBYTE * 1024
- GIGABYTE =
MEGABYTE * 1024
- TERABYTE =
GIGABYTE * 1024
- PETABYTE =
TERABYTE * 1024
- EXABYTE =
PETABYTE * 1024
Instance Method Summary collapse
- #bytes ⇒ Object (also: #byte)
- #exabytes ⇒ Object (also: #exabyte)
- #gigabytes ⇒ Object (also: #gigabyte)
- #kilobytes ⇒ Object (also: #kilobyte)
- #megabytes ⇒ Object (also: #megabyte)
-
#percent ⇒ Numeric
Translates a percentage number to a decimal.
- #petabytes ⇒ Object (also: #petabyte)
-
#sanity_check_max(maximum) ⇒ Numeric
Checks that the number is at most a given maximum.
-
#sanity_check_min(minimum) ⇒ Numeric
Checks that the number is at least a given minimum.
- #terabytes ⇒ Object (also: #terabyte)
-
#to_currency(opts = {}) ⇒ String
Formats a number as a currency string.
Instance Method Details
#bytes ⇒ Object Also known as: byte
9 10 11 |
# File 'lib/nutella/core_ext/numeric/bytes.rb', line 9 def bytes self end |
#exabytes ⇒ Object Also known as: exabyte
39 40 41 |
# File 'lib/nutella/core_ext/numeric/bytes.rb', line 39 def exabytes self * EXABYTE end |
#gigabytes ⇒ Object Also known as: gigabyte
24 25 26 |
# File 'lib/nutella/core_ext/numeric/bytes.rb', line 24 def gigabytes self * GIGABYTE end |
#kilobytes ⇒ Object Also known as: kilobyte
14 15 16 |
# File 'lib/nutella/core_ext/numeric/bytes.rb', line 14 def kilobytes self * KILOBYTE end |
#megabytes ⇒ Object Also known as: megabyte
19 20 21 |
# File 'lib/nutella/core_ext/numeric/bytes.rb', line 19 def megabytes self * MEGABYTE end |
#percent ⇒ Numeric
Translates a percentage number to a decimal.
5.percent # => 0.05
20.percent # => 0.2
25.percent # => 0.25
125.percent # => 1.25
100.percent # => 1
200.percent # => 2
12 13 14 |
# File 'lib/nutella/core_ext/numeric/percent.rb', line 12 def percent self / 100.0 end |
#petabytes ⇒ Object Also known as: petabyte
34 35 36 |
# File 'lib/nutella/core_ext/numeric/bytes.rb', line 34 def petabytes self * PETABYTE end |
#sanity_check_max(maximum) ⇒ Numeric
Checks that the number is at most a given maximum.
20 21 22 |
# File 'lib/nutella/core_ext/numeric/sanity_check.rb', line 20 def sanity_check_max(maximum) [self, maximum].min end |
#sanity_check_min(minimum) ⇒ Numeric
Checks that the number is at least a given minimum.
9 10 11 |
# File 'lib/nutella/core_ext/numeric/sanity_check.rb', line 9 def sanity_check_min(minimum) [self, minimum].max end |
#terabytes ⇒ Object Also known as: terabyte
29 30 31 |
# File 'lib/nutella/core_ext/numeric/bytes.rb', line 29 def terabytes self * TERABYTE end |
#to_currency(opts = {}) ⇒ String
Formats a number as a currency string.
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/nutella/core_ext/numeric/format.rb', line 13 def to_currency(opts = {}) opts = { sign: "$", position: :left, precision: 2, separator: ".", delimiter: ",", pretty: false }.merge(opts) opts[:precision] = 0 if opts[:pretty] && (self - self.floor).round(3) < 0.005 # Must round it outside also as the %f formatter always rounds down ("%.#{opts[:precision]}f" % self.round(opts[:precision])). # Apply thousands delimiter gsub(/(\d)(?=(\d{3})+(?!\d))/, "\\1#{opts[:delimiter]}"). # Apply unit separator gsub(/\.(\d{#{opts[:precision]}})$/, "#{opts[:separator]}\\1"). # Now put the sign on with #prepend or #concat depending on which side send( {left: :prepend, right: :concat}[opts[:position]], opts[:sign] || "" ) end |