Module: ActionView::Helpers::NumberHelper

Included in:
ActionView::Helpers
Defined in:
lib/action_view/helpers/number_helper.rb

Overview

Action View Number Helpers

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

Most methods expect a number argument, and will return it unchanged if can’t be converted into a valid number.

Defined Under Namespace

Classes: InvalidNumberError

Instance Method Summary collapse

Instance Method Details

#number_to_currency(number, options = {}) ⇒ Object

Delegates to ActiveSupport::NumberHelper#number_to_currency.

Additionally, supports a :raise option that will cause InvalidNumberError to be raised if number is not a valid number:

number_to_currency("12x34")              # => "$12x34"
number_to_currency("12x34", raise: true) # => InvalidNumberError


51
52
53
# File 'lib/action_view/helpers/number_helper.rb', line 51

def number_to_currency(number, options = {})
  delegate_number_helper_method(:number_to_currency, number, options)
end

#number_to_human(number, options = {}) ⇒ Object

Delegates to ActiveSupport::NumberHelper#number_to_human.

Additionally, supports a :raise option that will cause InvalidNumberError to be raised if number is not a valid number:

number_to_human("12x34")              # => "12x34"
number_to_human("12x34", raise: true) # => InvalidNumberError


111
112
113
# File 'lib/action_view/helpers/number_helper.rb', line 111

def number_to_human(number, options = {})
  delegate_number_helper_method(:number_to_human, number, options)
end

#number_to_human_size(number, options = {}) ⇒ Object

Delegates to ActiveSupport::NumberHelper#number_to_human_size.

Additionally, supports a :raise option that will cause InvalidNumberError to be raised if number is not a valid number:

number_to_human_size("12x34")              # => "12x34"
number_to_human_size("12x34", raise: true) # => InvalidNumberError


99
100
101
# File 'lib/action_view/helpers/number_helper.rb', line 99

def number_to_human_size(number, options = {})
  delegate_number_helper_method(:number_to_human_size, number, options)
end

#number_to_percentage(number, options = {}) ⇒ Object

Delegates to ActiveSupport::NumberHelper#number_to_percentage.

Additionally, supports a :raise option that will cause InvalidNumberError to be raised if number is not a valid number:

number_to_percentage("99x")              # => "99x%"
number_to_percentage("99x", raise: true) # => InvalidNumberError


63
64
65
# File 'lib/action_view/helpers/number_helper.rb', line 63

def number_to_percentage(number, options = {})
  delegate_number_helper_method(:number_to_percentage, number, options)
end

#number_to_phone(number, options = {}) ⇒ Object

Delegates to ActiveSupport::NumberHelper#number_to_phone.

Additionally, supports a :raise option that will cause InvalidNumberError to be raised if number is not a valid number:

number_to_phone("12x34")              # => "12x34"
number_to_phone("12x34", raise: true) # => InvalidNumberError


35
36
37
38
39
40
41
# File 'lib/action_view/helpers/number_helper.rb', line 35

def number_to_phone(number, options = {})
  return unless number
  options = options.symbolize_keys

  parse_float(number, true) if options.delete(:raise)
  ERB::Util.html_escape(ActiveSupport::NumberHelper.number_to_phone(number, options))
end

#number_with_delimiter(number, options = {}) ⇒ Object

Delegates to ActiveSupport::NumberHelper#number_to_delimited.

Additionally, supports a :raise option that will cause InvalidNumberError to be raised if number is not a valid number:

number_with_delimiter("12x34")              # => "12x34"
number_with_delimiter("12x34", raise: true) # => InvalidNumberError


75
76
77
# File 'lib/action_view/helpers/number_helper.rb', line 75

def number_with_delimiter(number, options = {})
  delegate_number_helper_method(:number_to_delimited, number, options)
end

#number_with_precision(number, options = {}) ⇒ Object

Delegates to ActiveSupport::NumberHelper#number_to_rounded.

Additionally, supports a :raise option that will cause InvalidNumberError to be raised if number is not a valid number:

number_with_precision("12x34")              # => "12x34"
number_with_precision("12x34", raise: true) # => InvalidNumberError


87
88
89
# File 'lib/action_view/helpers/number_helper.rb', line 87

def number_with_precision(number, options = {})
  delegate_number_helper_method(:number_to_rounded, number, options)
end