Class: Numeric

Inherits:
Object show all
Extended by:
Forwardable
Includes:
ActiveSupport::NumberHelper
Defined in:
lib/ruby-rails-extensions/extensions/to_dec.rb,
lib/ruby-rails-extensions/extensions/to_bool.rb,
lib/ruby-rails-extensions/extensions/to_money.rb,
lib/ruby-rails-extensions/extensions/usd_to_f.rb,
lib/ruby-rails-extensions/extensions/to_delimited.rb,
lib/ruby-rails-extensions/extensions/to_nonzero_i.rb,
lib/ruby-rails-extensions/extensions/to_negative_i.rb,
lib/ruby-rails-extensions/extensions/to_percentage.rb,
lib/ruby-rails-extensions/extensions/to_positive_i.rb

Instance Method Summary collapse

Instance Method Details

#to_dec(p = 4) ⇒ Object

:nodoc:



12
13
14
# File 'lib/ruby-rails-extensions/extensions/to_dec.rb', line 12

def to_dec(p = 4)
  (to_f / 100.0).round(p)
end

#to_money(options = { precision: 2, negative_format: '%u(%n)' }) ⇒ String

Converts a number to a money string

Parameters:

  • options (Hash) (defaults to: { precision: 2, negative_format: '%u(%n)' })

Returns:

See Also:

  • ActiveSupport::NumberHelper#number_to_currency


17
18
19
# File 'lib/ruby-rails-extensions/extensions/to_money.rb', line 17

def to_money(options = { precision: 2, negative_format: '%u(%n)' })
  number_to_currency(to_d, options)
end

#to_negative_iInteger, Nil

Calls to_i and returns the value if it is negative

Returns:

  • (Integer, Nil)


22
23
24
25
26
27
28
# File 'lib/ruby-rails-extensions/extensions/to_negative_i.rb', line 22

def to_negative_i
  val = to_i

  return unless val.negative?

  val
end

#to_nonzero_iInteger, Nil

Calls to_i and returns the value if it is nonzero

Returns:

  • (Integer, Nil)


22
23
24
25
26
27
28
# File 'lib/ruby-rails-extensions/extensions/to_nonzero_i.rb', line 22

def to_nonzero_i
  val = to_i

  return if val.zero?

  val
end

#to_percentage(options = { precision: 2 }) ⇒ String Also known as: to_per

Converts an #Integer, #Float, or #BigDecimal into a percentage string.

Parameters:

  • options (Hash) (defaults to: { precision: 2 })

Returns:

See Also:

  • ActiveSupport::NumberHelper#number_to_percentage


15
16
17
18
19
20
21
# File 'lib/ruby-rails-extensions/extensions/to_percentage.rb', line 15

def to_percentage(options = { precision: 2 })
  num = to_d * 100

  number_to_percentage(num, options)
rescue
  "#{(to_f * 100).round(options.fetch(:precision, 2))}%"
end

#to_positive_iInteger, Nil

Calls to_i and returns the value if it is positive

Returns:

  • (Integer, Nil)


22
23
24
25
26
27
28
# File 'lib/ruby-rails-extensions/extensions/to_positive_i.rb', line 22

def to_positive_i
  val = to_i

  return unless val.positive?

  val
end

#usd_to_fObject

:nodoc:



12
13
14
# File 'lib/ruby-rails-extensions/extensions/usd_to_f.rb', line 12

def usd_to_f
  to_f
end