跳至内容 跳至搜索

Action View 数字帮助器

提供将数字转换为格式化字符串的方法。提供用于电话号码、货币、百分比、精度、位置表示法、文件大小和漂亮打印的方法。

大多数方法都期望一个number参数,如果无法将其转换为有效数字,则会返回原始数字。

命名空间
方法
N

实例公共方法

number_to_currency(number, options = {})

委托给 ActiveSupport::NumberHelper#number_to_currency.

此外,支持一个:raise选项,如果number不是有效的数字,则会引发 InvalidNumberError

number_to_currency("12x34")              # => "$12x34"
number_to_currency("12x34", raise: true) # => InvalidNumberError
# File actionview/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 = {})

委托给 ActiveSupport::NumberHelper#number_to_human.

此外,支持一个:raise选项,如果number不是有效的数字,则会引发 InvalidNumberError

number_to_human("12x34")              # => "12x34"
number_to_human("12x34", raise: true) # => InvalidNumberError
# File actionview/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 = {})

委托给 ActiveSupport::NumberHelper#number_to_human_size.

此外,支持一个:raise选项,如果number不是有效的数字,则会引发 InvalidNumberError

number_to_human_size("12x34")              # => "12x34"
number_to_human_size("12x34", raise: true) # => InvalidNumberError
# File actionview/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 = {})

委托给 ActiveSupport::NumberHelper#number_to_percentage.

此外,支持一个:raise选项,如果number不是有效的数字,则会引发 InvalidNumberError

number_to_percentage("99x")              # => "99x%"
number_to_percentage("99x", raise: true) # => InvalidNumberError
# File actionview/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 = {})

委托给 ActiveSupport::NumberHelper#number_to_phone.

此外,支持一个:raise选项,如果number不是有效的数字,则会引发 InvalidNumberError

number_to_phone("12x34")              # => "12x34"
number_to_phone("12x34", raise: true) # => InvalidNumberError
# File actionview/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 = {})

委托给 ActiveSupport::NumberHelper#number_to_delimited.

此外,支持一个:raise选项,如果number不是有效的数字,则会引发 InvalidNumberError

number_with_delimiter("12x34")              # => "12x34"
number_with_delimiter("12x34", raise: true) # => InvalidNumberError
# File actionview/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 = {})

委托给 ActiveSupport::NumberHelper#number_to_rounded.

此外,支持一个:raise选项,如果number不是有效的数字,则会引发 InvalidNumberError

number_with_precision("12x34")              # => "12x34"
number_with_precision("12x34", raise: true) # => InvalidNumberError
# File actionview/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