Module: ActionView::Helpers::NumberHelper
Relationships & Source Files | |
Namespace Children | |
Exceptions:
| |
Extension / Inclusion / Inheritance Descendants | |
Included In:
ActionDispatch::DebugView,
::ActionView::Base ,
::ActionView::Helpers ,
::ActionView::TestCase ,
::ActionView::TestCase::Behavior
| |
Defined in: | actionview/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.
Instance Method Summary
-
#number_to_currency(number, options = {})
Delegates to ActiveSupport::NumberHelper#number_to_currency.
-
#number_to_human(number, options = {})
Delegates to ActiveSupport::NumberHelper#number_to_human.
-
#number_to_human_size(number, options = {})
Delegates to ActiveSupport::NumberHelper#number_to_human_size.
-
#number_to_percentage(number, options = {})
Delegates to ActiveSupport::NumberHelper#number_to_percentage.
-
#number_to_phone(number, options = {})
Delegates to ActiveSupport::NumberHelper#number_to_phone.
-
#number_with_delimiter(number, options = {})
Delegates to ActiveSupport::NumberHelper#number_to_delimited.
-
#number_with_precision(number, options = {})
Delegates to ActiveSupport::NumberHelper#number_to_rounded.
Instance Method Details
#number_to_currency(number, options = {})
Delegates to ActiveSupport::NumberHelper#number_to_currency.
number_to_currency("1234") # => "$1234.00"
Additionally, supports a :raise
option that will cause NumberHelper::InvalidNumberError
to be raised if number
is not a valid number:
number_to_currency("12x34") # => "$12x34"
number_to_currency("12x34", raise: true) # => InvalidNumberError
# File 'actionview/lib/action_view/helpers/number_helper.rb', line 55
def number_to_currency(number, = {}) delegate_number_helper_method(:number_to_currency, number, ) end
#number_to_human(number, options = {})
Delegates to ActiveSupport::NumberHelper#number_to_human.
number_to_human("1234") # => "1.23 Thousand"
Additionally, supports a :raise
option that will cause NumberHelper::InvalidNumberError
to be raised if number
is not a valid number:
number_to_human("12x34") # => "12x34"
number_to_human("12x34", raise: true) # => InvalidNumberError
# File 'actionview/lib/action_view/helpers/number_helper.rb', line 125
def number_to_human(number, = {}) delegate_number_helper_method(:number_to_human, number, ) end
#number_to_human_size(number, options = {})
Delegates to ActiveSupport::NumberHelper#number_to_human_size.
number_to_human_size("1234") # => "1.21 KB"
Additionally, supports a :raise
option that will cause NumberHelper::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
# File 'actionview/lib/action_view/helpers/number_helper.rb', line 111
def number_to_human_size(number, = {}) delegate_number_helper_method(:number_to_human_size, number, ) end
#number_to_percentage(number, options = {})
Delegates to ActiveSupport::NumberHelper#number_to_percentage.
number_to_percentage("99") # => "99.000%"
Additionally, supports a :raise
option that will cause NumberHelper::InvalidNumberError
to be raised if number
is not a valid number:
number_to_percentage("99x") # => "99x%"
number_to_percentage("99x", raise: true) # => InvalidNumberError
# File 'actionview/lib/action_view/helpers/number_helper.rb', line 69
def number_to_percentage(number, = {}) delegate_number_helper_method(:number_to_percentage, number, ) end
#number_to_phone(number, options = {})
Delegates to ActiveSupport::NumberHelper#number_to_phone.
number_to_phone("1234567890") # => "123-456-7890"
Additionally, supports a :raise
option that will cause NumberHelper::InvalidNumberError
to be raised if number
is not a valid number:
number_to_phone("12x34") # => "12x34"
number_to_phone("12x34", raise: true) # => InvalidNumberError
# File 'actionview/lib/action_view/helpers/number_helper.rb', line 37
def number_to_phone(number, = {}) return unless number = .symbolize_keys parse_float(number, true) if .delete(:raise) ERB::Util.html_escape(ActiveSupport::NumberHelper.number_to_phone(number, )) end
#number_with_delimiter(number, options = {})
Delegates to ActiveSupport::NumberHelper#number_to_delimited.
number_with_delimiter("1234") # => "1,234"
Additionally, supports a :raise
option that will cause NumberHelper::InvalidNumberError
to be raised if number
is not a valid number:
number_with_delimiter("12x34") # => "12x34"
number_with_delimiter("12x34", raise: true) # => InvalidNumberError
# File 'actionview/lib/action_view/helpers/number_helper.rb', line 83
def number_with_delimiter(number, = {}) delegate_number_helper_method(:number_to_delimited, number, ) end
#number_with_precision(number, options = {})
Delegates to ActiveSupport::NumberHelper#number_to_rounded.
number_with_precision("1234") # => "1234.000"
Additionally, supports a :raise
option that will cause NumberHelper::InvalidNumberError
to be raised if number
is not a valid number:
number_with_precision("12x34") # => "12x34"
number_with_precision("12x34", raise: true) # => InvalidNumberError
# File 'actionview/lib/action_view/helpers/number_helper.rb', line 97
def number_with_precision(number, = {}) delegate_number_helper_method(:number_to_rounded, number, ) end