| Module | ActionView::Helpers::NumberHelper |
| In: |
vendor/rails/actionpack/lib/action_view/helpers/number_helper.rb
|
Provides methods for converting a number into a formatted string that currently represents one of the following forms: phone number, percentage, money, or precision level.
Formates a number into a currency string. The options hash can be used to customize the format of the output. The number can contain a level of precision using the precision key; default is 2 The currency type can be set using the unit key; default is "$" The unit separator can be set using the separator key; default is "." The delimiter can be set using the delimiter key; default is "," Examples:
number_to_currency(1234567890.50) => $1,234,567,890.50
number_to_currency(1234567890.506) => $1,234,567,890.51
number_to_currency(1234567890.50, {:unit => "£", :separator => ",", :delimiter => ""}) => £1234567890,50
# File vendor/rails/actionpack/lib/action_view/helpers/number_helper.rb, line 37
37: def number_to_currency(number, options = {})
38: options = options.stringify_keys
39: precision, unit, separator, delimiter = options.delete("precision") { 2 }, options.delete("unit") { "$" }, options.delete("separator") { "." }, options.delete("delimiter") { "," }
40: begin
41: parts = number_with_precision(number, precision).split('.')
42: unit + number_with_delimiter(parts[0], delimiter) + separator + parts[1].to_s
43: rescue
44: number
45: end
46: end
Returns a formatted-for-humans file size.
Examples:
human_size(123) => 123 Bytes human_size(1234) => 1.2 KB human_size(12345) => 12.1 KB human_size(1234567) => 1.2 MB human_size(1234567890) => 1.1 GB
# File vendor/rails/actionpack/lib/action_view/helpers/number_helper.rb, line 86
86: def number_to_human_size(size)
87: begin
88: return "%d Bytes" % size if size < 1.kilobytes
89: return "%.1f KB" % (size/1.0.kilobytes) if size < 1.megabytes
90: return "%.1f MB" % (size/1.0.megabytes) if size < 1.gigabytes
91: return "%.1f GB" % (size/1.0.gigabytes) if size < 1.terabytes
92: return "%.1f TB" % (size/1.0.terabytes)
93: rescue
94: # just return nothing
95: end
96: end
Formats a number as into a percentage string. The options hash can be used to customize the format of the output. The number can contain a level of precision using the precision key; default is 3 The unit separator can be set using the separator key; default is "." Examples:
number_to_percentage(100) => 100.000%
number_to_percentage(100, {:precision => 0}) => 100%
number_to_percentage(302.0574, {:precision => 2}) => 302.06%
# File vendor/rails/actionpack/lib/action_view/helpers/number_helper.rb, line 55
55: def number_to_percentage(number, options = {})
56: options = options.stringify_keys
57: precision, separator = options.delete("precision") { 3 }, options.delete("separator") { "." }
58: begin
59: number = number_with_precision(number, precision)
60: parts = number.split('.')
61: if parts.at(1).nil?
62: parts[0] + "%"
63: else
64: parts[0] + separator + parts[1].to_s + "%"
65: end
66: rescue
67: number
68: end
69: end
Formats a number into a US phone number string. The options can be hash used to customize the format of the output. The area code can be surrounded by parenthesis by setting +:area_code+ to true; default is false The delimiter can be set using +:delimiter+; default is "-" Examples:
number_to_phone(1235551234) => 123-555-1234
number_to_phone(1235551234, {:area_code => true}) => (123) 555-1234
number_to_phone(1235551234, {:delimiter => " "}) => 123 555 1234
number_to_phone(1235551234, {:area_code => true, :extension => 555}) => (123) 555-1234 x 555
# File vendor/rails/actionpack/lib/action_view/helpers/number_helper.rb, line 15
15: def number_to_phone(number, options = {})
16: options = options.stringify_keys
17: area_code = options.delete("area_code") { false }
18: delimiter = options.delete("delimiter") { "-" }
19: extension = options.delete("extension") { "" }
20: begin
21: str = area_code == true ? number.to_s.gsub(/([0-9]{3})([0-9]{3})([0-9]{4})/,"(\\1) \\2#{delimiter}\\3") : number.to_s.gsub(/([0-9]{3})([0-9]{3})([0-9]{4})/,"\\1#{delimiter}\\2#{delimiter}\\3")
22: extension.to_s.strip.empty? ? str : "#{str} x #{extension.to_s.strip}"
23: rescue
24: number
25: end
26: end
Formats a number with a delimiter. Example:
number_with_delimiter(12345678) => 12,345,678
# File vendor/rails/actionpack/lib/action_view/helpers/number_helper.rb, line 74
74: def number_with_delimiter(number, delimiter=",")
75: number.to_s.gsub(/(\d)(?=(\d\d\d)+(?!\d))/, "\\1#{delimiter}")
76: end