edge badge
Methods
B
D
E
F
G
H
I
K
M
P
S
T
W
Constants
KILOBYTE = 1024
 
MEGABYTE = KILOBYTE * 1024
 
GIGABYTE = MEGABYTE * 1024
 
TERABYTE = GIGABYTE * 1024
 
PETABYTE = TERABYTE * 1024
 
EXABYTE = PETABYTE * 1024
 
Instance Public methods
byte()
Alias for: bytes
bytes()

Enables the use of byte calculations and declarations, like 45.bytes + 2.6.megabytes

2.bytes # => 2
Also aliased as: byte
# File activesupport/lib/active_support/core_ext/numeric/bytes.rb, line 12
def bytes
  self
end
day()
Alias for: days
days()

Returns a Duration instance matching the number of days provided.

2.days # => 2 days
Also aliased as: day
# File activesupport/lib/active_support/core_ext/numeric/time.rb, line 60
def days
  ActiveSupport::Duration.new(self * 24.hours, [[:days, self]])
end
duplicable?()

Numbers are not duplicable:

3.duplicable? # => false
3.dup         # => TypeError: can't dup Fixnum
# File activesupport/lib/active_support/core_ext/object/duplicable.rb, line 74
def duplicable?
  false
end
exabyte()
Alias for: exabytes
exabytes()

Returns the number of bytes equivalent to the exabytes provided.

2.exabytes # => 2_305_843_009_213_693_952
Also aliased as: exabyte
# File activesupport/lib/active_support/core_ext/numeric/bytes.rb, line 60
def exabytes
  self * EXABYTE
end
fortnight()
Alias for: fortnights
fortnights()

Returns a Duration instance matching the number of fortnights provided.

2.fortnights # => 28 days
Also aliased as: fortnight
# File activesupport/lib/active_support/core_ext/numeric/time.rb, line 76
def fortnights
  ActiveSupport::Duration.new(self * 2.weeks, [[:days, self * 14]])
end
gigabyte()
Alias for: gigabytes
gigabytes()

Returns the number of bytes equivalent to the gigabytes provided.

2.gigabytes # => 2_147_483_648
Also aliased as: gigabyte
# File activesupport/lib/active_support/core_ext/numeric/bytes.rb, line 36
def gigabytes
  self * GIGABYTE
end
hour()
Alias for: hours
hours()

Returns a Duration instance matching the number of hours provided.

2.hours # => 7_200 seconds
Also aliased as: hour
# File activesupport/lib/active_support/core_ext/numeric/time.rb, line 52
def hours
  ActiveSupport::Duration.new(self * 3600, [[:seconds, self * 3600]])
end
html_safe?()
# File activesupport/lib/active_support/core_ext/string/output_safety.rb, line 125
def html_safe?
  true
end
in_milliseconds()

Returns the number of milliseconds equivalent to the seconds provided. Used with the standard time durations, like 1.hour.in_milliseconds – so we can feed them to JavaScript functions like getTime().

2.in_milliseconds # => 2_000
# File activesupport/lib/active_support/core_ext/numeric/time.rb, line 86
def in_milliseconds
  self * 1000
end
kilobyte()
Alias for: kilobytes
kilobytes()

Returns the number of bytes equivalent to the kilobytes provided.

2.kilobytes # => 2048
Also aliased as: kilobyte
# File activesupport/lib/active_support/core_ext/numeric/bytes.rb, line 20
def kilobytes
  self * KILOBYTE
end
megabyte()
Alias for: megabytes
megabytes()

Returns the number of bytes equivalent to the megabytes provided.

2.megabytes # => 2_097_152
Also aliased as: megabyte
# File activesupport/lib/active_support/core_ext/numeric/bytes.rb, line 28
def megabytes
  self * MEGABYTE
end
minute()
Alias for: minutes
minutes()

Returns a Duration instance matching the number of minutes provided.

2.minutes # => 120 seconds
Also aliased as: minute
# File activesupport/lib/active_support/core_ext/numeric/time.rb, line 44
def minutes
  ActiveSupport::Duration.new(self * 60, [[:seconds, self * 60]])
end
petabyte()
Alias for: petabytes
petabytes()

Returns the number of bytes equivalent to the petabytes provided.

2.petabytes # => 2_251_799_813_685_248
Also aliased as: petabyte
# File activesupport/lib/active_support/core_ext/numeric/bytes.rb, line 52
def petabytes
  self * PETABYTE
end
second()
Alias for: seconds
seconds()

Enables the use of time calculations and declarations, like 45.minutes + 2.hours + 4.years.

These methods use Time#advance for precise date calculations when using from_now, ago, etc. as well as adding or subtracting their results from a Time object. For example:

# equivalent to Time.current.advance(months: 1)
1.month.from_now

# equivalent to Time.current.advance(years: 2)
2.years.from_now

# equivalent to Time.current.advance(months: 4, years: 5)
(4.months + 5.years).from_now

While these methods provide precise calculation when used as in the examples above, care should be taken to note that this is not true if the result of `months', `years', etc is converted before use:

# equivalent to 30.days.to_i.from_now
1.month.to_i.from_now

# equivalent to 365.25.days.to_f.from_now
1.year.to_f.from_now

In such cases, Ruby's core Date and Time should be used for precision date and time arithmetic.

Also aliased as: second
# File activesupport/lib/active_support/core_ext/numeric/time.rb, line 36
def seconds
  ActiveSupport::Duration.new(self, [[:seconds, self]])
end
terabyte()
Alias for: terabytes
terabytes()

Returns the number of bytes equivalent to the terabytes provided.

2.terabytes # => 2_199_023_255_552
Also aliased as: terabyte
# File activesupport/lib/active_support/core_ext/numeric/bytes.rb, line 44
def terabytes
  self * TERABYTE
end
to_formatted_s(format = :default, options = {})

Provides options for converting numbers into formatted strings. Options are provided for phone numbers, currency, percentage, precision, positional notation, file size and pretty printing.

Options

For details on which formats use which options, see ActiveSupport::NumberHelper

Examples

Phone Numbers:
5551234.to_s(:phone)                                     # => 555-1234
1235551234.to_s(:phone)                                  # => 123-555-1234
1235551234.to_s(:phone, area_code: true)                 # => (123) 555-1234
1235551234.to_s(:phone, delimiter: ' ')                  # => 123 555 1234
1235551234.to_s(:phone, area_code: true, extension: 555) # => (123) 555-1234 x 555
1235551234.to_s(:phone, country_code: 1)                 # => +1-123-555-1234
1235551234.to_s(:phone, country_code: 1, extension: 1343, delimiter: '.')
# => +1.123.555.1234 x 1343

Currency:
1234567890.50.to_s(:currency)                 # => $1,234,567,890.50
1234567890.506.to_s(:currency)                # => $1,234,567,890.51
1234567890.506.to_s(:currency, precision: 3)  # => $1,234,567,890.506
1234567890.506.to_s(:currency, locale: :fr)   # => 1 234 567 890,51 €
-1234567890.50.to_s(:currency, negative_format: '(%u%n)')
# => ($1,234,567,890.50)
1234567890.50.to_s(:currency, unit: '£', separator: ',', delimiter: '')
# => £1234567890,50
1234567890.50.to_s(:currency, unit: '£', separator: ',', delimiter: '', format: '%n %u')
# => 1234567890,50 £

Percentage:
100.to_s(:percentage)                                  # => 100.000%
100.to_s(:percentage, precision: 0)                    # => 100%
1000.to_s(:percentage, delimiter: '.', separator: ',') # => 1.000,000%
302.24398923423.to_s(:percentage, precision: 5)        # => 302.24399%
1000.to_s(:percentage, locale: :fr)                    # => 1 000,000%
100.to_s(:percentage, format: '%n  %')                 # => 100  %

Delimited:
12345678.to_s(:delimited)                     # => 12,345,678
12345678.05.to_s(:delimited)                  # => 12,345,678.05
12345678.to_s(:delimited, delimiter: '.')     # => 12.345.678
12345678.to_s(:delimited, delimiter: ',')     # => 12,345,678
12345678.05.to_s(:delimited, separator: ' ')  # => 12,345,678 05
12345678.05.to_s(:delimited, locale: :fr)     # => 12 345 678,05
98765432.98.to_s(:delimited, delimiter: ' ', separator: ',')
# => 98 765 432,98

Rounded:
111.2345.to_s(:rounded)                                      # => 111.235
111.2345.to_s(:rounded, precision: 2)                        # => 111.23
13.to_s(:rounded, precision: 5)                              # => 13.00000
389.32314.to_s(:rounded, precision: 0)                       # => 389
111.2345.to_s(:rounded, significant: true)                   # => 111
111.2345.to_s(:rounded, precision: 1, significant: true)     # => 100
13.to_s(:rounded, precision: 5, significant: true)           # => 13.000
111.234.to_s(:rounded, locale: :fr)                          # => 111,234
13.to_s(:rounded, precision: 5, significant: true, strip_insignificant_zeros: true)
# => 13
389.32314.to_s(:rounded, precision: 4, significant: true)    # => 389.3
1111.2345.to_s(:rounded, precision: 2, separator: ',', delimiter: '.')
# => 1.111,23

Human-friendly size in Bytes:
123.to_s(:human_size)                                   # => 123 Bytes
1234.to_s(:human_size)                                  # => 1.21 KB
12345.to_s(:human_size)                                 # => 12.1 KB
1234567.to_s(:human_size)                               # => 1.18 MB
1234567890.to_s(:human_size)                            # => 1.15 GB
1234567890123.to_s(:human_size)                         # => 1.12 TB
1234567.to_s(:human_size, precision: 2)                 # => 1.2 MB
483989.to_s(:human_size, precision: 2)                  # => 470 KB
1234567.to_s(:human_size, precision: 2, separator: ',') # => 1,2 MB
1234567890123.to_s(:human_size, precision: 5)           # => "1.1229 TB"
524288000.to_s(:human_size, precision: 5)               # => "500 MB"

Human-friendly format:
123.to_s(:human)                                       # => "123"
1234.to_s(:human)                                      # => "1.23 Thousand"
12345.to_s(:human)                                     # => "12.3 Thousand"
1234567.to_s(:human)                                   # => "1.23 Million"
1234567890.to_s(:human)                                # => "1.23 Billion"
1234567890123.to_s(:human)                             # => "1.23 Trillion"
1234567890123456.to_s(:human)                          # => "1.23 Quadrillion"
1234567890123456789.to_s(:human)                       # => "1230 Quadrillion"
489939.to_s(:human, precision: 2)                      # => "490 Thousand"
489939.to_s(:human, precision: 4)                      # => "489.9 Thousand"
1234567.to_s(:human, precision: 4,
                 significant: false)                   # => "1.2346 Million"
1234567.to_s(:human, precision: 1,
                 separator: ',',
                 significant: false)                   # => "1,2 Million"
# File activesupport/lib/active_support/core_ext/numeric/conversions.rb, line 100
def to_formatted_s(format = :default, options = {})
  case format
  when :phone
    return ActiveSupport::NumberHelper.number_to_phone(self, options)
  when :currency
    return ActiveSupport::NumberHelper.number_to_currency(self, options)
  when :percentage
    return ActiveSupport::NumberHelper.number_to_percentage(self, options)
  when :delimited
    return ActiveSupport::NumberHelper.number_to_delimited(self, options)
  when :rounded
    return ActiveSupport::NumberHelper.number_to_rounded(self, options)
  when :human
    return ActiveSupport::NumberHelper.number_to_human(self, options)
  when :human_size
    return ActiveSupport::NumberHelper.number_to_human_size(self, options)
  else
    self.to_default_s
  end
end
to_s(base_or_format = 10, options = nil)
# File activesupport/lib/active_support/core_ext/numeric/conversions.rb, line 124
def to_s(base_or_format = 10, options = nil)
  if base_or_format.is_a?(Symbol)
    to_formatted_s(base_or_format, options || {})
  else
    to_default_s(base_or_format)
  end
end
week()
Alias for: weeks
weeks()

Returns a Duration instance matching the number of weeks provided.

2.weeks # => 14 days
Also aliased as: week
# File activesupport/lib/active_support/core_ext/numeric/time.rb, line 68
def weeks
  ActiveSupport::Duration.new(self * 7.days, [[:days, self * 7]])
end