Class: Numeric

Inherits:
Object
  • Object
show all
Defined in:
documented/common/class_exts/numeric.rb

Overview

Extends the Numeric class to provide additional time and formatting methods. This class adds methods to convert numeric values into time formats and to format numbers with commas.

Examples:

Using the Numeric extensions

120.as_time # => "2:00:00"
123456.with_commas # => "123,456"

Instance Method Summary collapse

Instance Method Details

#as_timeString

Converts the numeric value to a time string in “HH:MM:SS” format.

Examples:

3661.as_time # => "1:01:01"

Returns:

  • (String)

    The time representation of the numeric value.



14
15
16
# File 'documented/common/class_exts/numeric.rb', line 14

def as_time
  sprintf("%d:%02d:%02d", (self / 60).truncate, self.truncate % 60, ((self % 1) * 60).truncate)
end

#daysNumeric Also known as: day

Converts the numeric value to days.

Examples:

1.days # => 86400

Returns:

  • (Numeric)

    The numeric value multiplied by 86400, representing days.



57
58
59
# File 'documented/common/class_exts/numeric.rb', line 57

def days
  return self * 86400
end

#hoursNumeric Also known as: hour

Converts the numeric value to hours.

Examples:

1.5.hours # => 5400

Returns:

  • (Numeric)

    The numeric value multiplied by 3600, representing hours.



48
49
50
# File 'documented/common/class_exts/numeric.rb', line 48

def hours
  return self * 3600
end

#minutesNumeric Also known as: minute

Converts the numeric value to minutes.

Examples:

2.minutes # => 120

Returns:

  • (Numeric)

    The numeric value multiplied by 60, representing minutes.



39
40
41
# File 'documented/common/class_exts/numeric.rb', line 39

def minutes
  return self * 60
end

#secondsNumeric Also known as: second

Returns the numeric value as seconds.

Examples:

5.seconds # => 5

Returns:

  • (Numeric)

    The original numeric value, representing seconds.



30
31
32
# File 'documented/common/class_exts/numeric.rb', line 30

def seconds
  return self
end

#with_commasString

Formats the numeric value as a string with commas separating thousands.

Examples:

1234567.with_commas # => "1,234,567"

Returns:

  • (String)

    The formatted string with commas.



22
23
24
# File 'documented/common/class_exts/numeric.rb', line 22

def with_commas
  self.to_s.reverse.scan(/(?:\d*\.)?\d{1,3}-?/).join(',').reverse
end