Class: Numeric
- 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
-
#to_dec(p = 4) ⇒ Object
:nodoc:.
-
#to_money(options = { precision: 2, negative_format: '%u(%n)' }) ⇒ String
Converts a number to a money string.
-
#to_negative_i ⇒ Integer, Nil
Calls
to_iand returns the value if it is negative. -
#to_nonzero_i ⇒ Integer, Nil
Calls
to_iand returns the value if it is nonzero. -
#to_percentage(options = { precision: 2 }) ⇒ String
(also: #to_per)
Converts an #Integer, #Float, or #BigDecimal into a percentage string.
-
#to_positive_i ⇒ Integer, Nil
Calls
to_iand returns the value if it is positive. -
#usd_to_f ⇒ Object
:nodoc:.
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
17 18 19 |
# File 'lib/ruby-rails-extensions/extensions/to_money.rb', line 17 def to_money( = { precision: 2, negative_format: '%u(%n)' }) number_to_currency(to_d, ) end |
#to_negative_i ⇒ Integer, Nil
Calls to_i and returns the value if it is negative
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_i ⇒ Integer, Nil
Calls to_i and returns the value if it is nonzero
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.
15 16 17 18 19 20 21 |
# File 'lib/ruby-rails-extensions/extensions/to_percentage.rb', line 15 def to_percentage( = { precision: 2 }) num = to_d * 100 number_to_percentage(num, ) rescue "#{(to_f * 100).round(.fetch(:precision, 2))}%" end |
#to_positive_i ⇒ Integer, Nil
Calls to_i and returns the value if it is positive
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_f ⇒ Object
:nodoc:
12 13 14 |
# File 'lib/ruby-rails-extensions/extensions/usd_to_f.rb', line 12 def usd_to_f to_f end |