Class: Numeric

Inherits:
Object show all
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

Instance Method Details

#bytesObject Also known as: byte



9
10
11
# File 'lib/nutella/core_ext/numeric/bytes.rb', line 9

def bytes
  self
end

#exabytesObject Also known as: exabyte



39
40
41
# File 'lib/nutella/core_ext/numeric/bytes.rb', line 39

def exabytes
  self * EXABYTE
end

#gigabytesObject Also known as: gigabyte



24
25
26
# File 'lib/nutella/core_ext/numeric/bytes.rb', line 24

def gigabytes
  self * GIGABYTE
end

#kilobytesObject Also known as: kilobyte



14
15
16
# File 'lib/nutella/core_ext/numeric/bytes.rb', line 14

def kilobytes
  self * KILOBYTE
end

#megabytesObject Also known as: megabyte



19
20
21
# File 'lib/nutella/core_ext/numeric/bytes.rb', line 19

def megabytes
  self * MEGABYTE
end

#percentNumeric

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

Returns:

  • (Numeric)

    the decimal for the percentage number



12
13
14
# File 'lib/nutella/core_ext/numeric/percent.rb', line 12

def percent
  self / 100.0
end

#petabytesObject 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.

Examples:

Makes sure that a variable num is a maximum of 5

num = num.sanity_check_max 5

Parameters:

  • maximum (Numeric)

    the maximum that the number can be

Returns:

  • (Numeric)

    what the number must be in order to satisfy the 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.

Examples:

Makes sure that a variable num is a minimum of 5

num = num.sanity_check_min 5

Parameters:

  • minimum (Numeric)

    the minimum that the number can be

Returns:

  • (Numeric)

    what the number must be in order to satisfy the minimum



9
10
11
# File 'lib/nutella/core_ext/numeric/sanity_check.rb', line 9

def sanity_check_min(minimum)
  [self, minimum].max
end

#terabytesObject 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.

Parameters:

  • opts (Hash) (defaults to: {})

    the options hash

Options Hash (opts):

  • :sign (String)

    the currency sign

  • :position (Symbol)

    the position of the currency sign

  • :precision (Integer)

    amount of decimal places to display

  • :separator (String)

    the separator between the ones and tenths

  • :delimiter (String)

    the thousands delimiter

  • :pretty (Boolean)

    sets 0 precision for whole amounts

Returns:

  • (String)

    the number in a currency format



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