跳到內文 跳到搜尋

數字輔助器

提供方法來將數字格式化成貨幣、百分比、電話號碼等。

類別中範例用法

class Topic
  include ActiveSupport::NumberHelper

  def price
    number_to_currency(@price)
  end
end

模組中範例用法

require "active_support/number_helper"

module NumberFormatting
  def format_price(price)
    ActiveSupport::NumberHelper.number_to_currency(price)
  end
end
方法
N

執行個體公開方法

number_to_currency(number, options = {})

數字格式化成一個貨幣字串。

number_to_currency(1234567890.50)  # => "$1,234,567,890.50"
number_to_currency(1234567890.506) # => "$1,234,567,890.51"
number_to_currency("12x34")        # => "$12x34"

number_to_currency(1234567890.50, unit: "£", separator: ",", delimiter: "")
# => "£1234567890,50"

當前區域的貨幣單位和數字格式將用於,除非選擇其他選項。不會執行任何貨幣轉換。假如使用者有辦法更改他們的區域,也將會能更改顯示在這個輔助器內的相對貨幣值。如果您的應用程式將支援多個區域,您可能想指定一個固定的:locale選項,或考慮使用一個能夠執行貨幣轉換的程式庫。

選項

:locale

用於格式化的區域。預設為當前區域。

number_to_currency(1234567890.506, locale: :fr)
# => "1 234 567 890,51 €"
:precision

精確度級別。預設為 2。

number_to_currency(1234567890.123, precision: 3) # => "$1,234,567,890.123"
number_to_currency(0.456789, precision: 0)       # => "$0"
:round_mode

指定進行捨入的方式。請參閱BigDecimal.mode。預設為:default

number_to_currency(1234567890.01, precision: 0, round_mode: :up)
# => "$1,234,567,891"
:unit

貨幣的面額。預設為("$")

:separator

小數點分隔號。預設為(".")

:delimiter

千位分隔號。預設為(",")

:format

非負數字的格式。%u代表貨幣,%n代表數字。預設為("%u%n")

number_to_currency(1234567890.50, format: "%n %u")
# => "1,234,567,890.50 $"
:negative_format

負數字的格式。%u%n行為與:format中相同,但是%n代表數字的絕對值。預設為:format的數值加上開頭的(-)

number_to_currency(-1234567890.50, negative_format: "(%u%n)")
# => "($1,234,567,890.50)"
:strip_insignificant_zeros

是否要移除小數點分隔號後不重要的零。預設為 false。

number_to_currency(1234567890.50, strip_insignificant_zeros: true)
# => "$1,234,567,890.5"
# File activesupport/lib/active_support/number_helper.rb, line 161
def number_to_currency(number, options = {})
  NumberToCurrencyConverter.convert(number, options)
end

number_to_delimited(number, options = {})

使用分隔號對數字進行千位分組。

number_to_delimited(12345678)      # => "12,345,678"
number_to_delimited("123456")      # => "123,456"
number_to_delimited(12345678.9876) # => "12,345,678.9876"
number_to_delimited("12x34")       # => "12x34"

number_to_delimited(12345678.9876, delimiter: ".", separator: ",")
# => "12.345.678,9876"

選項

:locale

用於格式化的區域。預設為當前區域。

number_to_delimited(12345678.05, locale: :fr)
# => "12 345 678,05"
:delimiter

千位分隔號。預設為(",")

number_to_delimited(12345678, delimiter: ".")
# => "12.345.678"
:separator

小數點分隔號。預設為(".")

number_to_delimited(12345678.05, separator: " ")
# => "12,345,678 05"
:delimiter_pattern

一個正規表示法,用於決定分隔號的位置。當使用涵蓋印度盧比等貨幣格式時,會有所幫助。

number_to_delimited("123456.78", delimiter_pattern: /(\d+?)(?=(\d\d)+(\d)(?!\d))/)
# => "1,23,456.78"
# File activesupport/lib/active_support/number_helper.rb, line 264
def number_to_delimited(number, options = {})
  NumberToDelimitedConverter.convert(number, options)
end

number_to_human(number, options = {})

格式化 number 成較人友善的表示形式。對於可能會變得非常龐大且難以閱讀的號碼很有用。

number_to_human(123)                 # => "123"
number_to_human(1234)                # => "1.23 Thousand"
number_to_human(12345)               # => "12.3 Thousand"
number_to_human(1234567)             # => "1.23 Million"
number_to_human(1234567890)          # => "1.23 Billion"
number_to_human(1234567890123)       # => "1.23 Trillion"
number_to_human(1234567890123456)    # => "1.23 Quadrillion"
number_to_human(1234567890123456789) # => "1230 Quadrillion"

如果您想漂亮列印檔案大小,請參閱 number_to_human_size

選項

:locale

用於格式化的區域。預設為當前區域。

:precision

精確度等級。預設為 3。

number_to_human(123456, precision: 2) # => "120 Thousand"
number_to_human(123456, precision: 4) # => "123.5 Thousand"
:round_mode

指定進行捨入的方式。請參閱BigDecimal.mode。預設為:default

number_to_human(123456, precision: 2, round_mode: :up)
# => "130 Thousand"
:significant

:precision 是否應套用至有效數字,而非小數位。預設為 true。

:separator

小數點分隔號。預設為(".")

number_to_human(123456, precision: 4, separator: ",")
# => "123,5 Thousand"
:delimiter

千位分隔號。預設為(",")

:strip_insignificant_zeros

是否移除小數點後的無意義零。預設為 true。

number_to_human(1000000)                                   # => "1 Million"
number_to_human(1000000, strip_insignificant_zeros: false) # => "1.00 Million"
number_to_human(10.01)                                     # => "10"
number_to_human(10.01, strip_insignificant_zeros: false)   # => "10.0"
:format

輸出的格式。%n 代表數字,而 %u 代表量化詞 (例如,「千」)。預設為 "%n %u"

:units

客製化單位量化詞名稱的 Hash

number_to_human(1, units: { unit: "m", thousand: "km" })        # => "1 m"
number_to_human(100, units: { unit: "m", thousand: "km" })      # => "100 m"
number_to_human(1000, units: { unit: "m", thousand: "km" })     # => "1 km"
number_to_human(100000, units: { unit: "m", thousand: "km" })   # => "100 km"
number_to_human(10000000, units: { unit: "m", thousand: "km" }) # => "10000 km"

支援以下用於整數單位的金鑰::unit:ten:hundred:thousand:million:billion:trillion:quadrillion。另外,支援以下用於小數單位的金鑰::deci:centi:mili:micro:nano:pico:femto

可以在 I18n 語系中將 Hash 定義為範圍。例如

en:
  distance:
    centi:
      one: "centimeter"
      other: "centimeters"
    unit:
      one: "meter"
      other: "meters"
    thousand:
      one: "kilometer"
      other: "kilometers"

然後可透過名稱指定

number_to_human(1, units: :distance)        # => "1 meter"
number_to_human(100, units: :distance)      # => "100 meters"
number_to_human(1000, units: :distance)     # => "1 kilometer"
number_to_human(100000, units: :distance)   # => "100 kilometers"
number_to_human(10000000, units: :distance) # => "10000 kilometers"
number_to_human(0.1, units: :distance)      # => "10 centimeters"
number_to_human(0.01, units: :distance)     # => "1 centimeter"
# File activesupport/lib/active_support/number_helper.rb, line 475
def number_to_human(number, options = {})
  NumberToHumanConverter.convert(number, options)
end

number_to_human_size(number, options = {})

number 格式化為位元組,以取得較人友善的表示形式。用於將檔案大小回報給使用者時很有用。

number_to_human_size(123)                 # => "123 Bytes"
number_to_human_size(1234)                # => "1.21 KB"
number_to_human_size(12345)               # => "12.1 KB"
number_to_human_size(1234567)             # => "1.18 MB"
number_to_human_size(1234567890)          # => "1.15 GB"
number_to_human_size(1234567890123)       # => "1.12 TB"
number_to_human_size(1234567890123456)    # => "1.1 PB"
number_to_human_size(1234567890123456789) # => "1.07 EB"

如果您想漂亮列印一般數字,請參閱 number_to_human

選項

:locale

用於格式化的區域。預設為當前區域。

:precision

精確度等級。預設為 3。

number_to_human_size(123456, precision: 2)  # => "120 KB"
number_to_human_size(1234567, precision: 2) # => "1.2 MB"
:round_mode

指定進行捨入的方式。請參閱BigDecimal.mode。預設為:default

number_to_human_size(123456, precision: 2, round_mode: :up)
# => "130 KB"
:significant

:precision 是否應套用至有效數字,而非小數位。預設為 true。

:separator

小數點分隔號。預設為(".")

number_to_human_size(1234567, separator: ",")
# => "1,18 MB"
:delimiter

千位分隔號。預設為(",")

:strip_insignificant_zeros

是否移除小數點後的無意義零。預設為 true。

# File activesupport/lib/active_support/number_helper.rb, line 373
def number_to_human_size(number, options = {})
  NumberToHumanSizeConverter.convert(number, options)
end

number_to_percentage(number, options = {})

number 格式化為百分比字串。

number_to_percentage(100)   # => "100.000%"
number_to_percentage("99")  # => "99.000%"
number_to_percentage("99x") # => "99x%"

number_to_percentage(12345.6789, delimiter: ".", separator: ",", precision: 2)
# => "12.345,68%"

選項

:locale

用於格式化的區域。預設為當前區域。

number_to_percentage(1000, locale: :fr)
# => "1000,000%"
:precision

精確度等級,或 nil 以保留 number 的精確度。預設為 2。

number_to_percentage(12.3456789, precision: 4) # => "12.3457%"
number_to_percentage(99.999, precision: 0)     # => "100%"
number_to_percentage(99.999, precision: nil)   # => "99.999%"
:round_mode

指定進行捨入的方式。請參閱BigDecimal.mode。預設為:default

number_to_percentage(12.3456789, precision: 4, round_mode: :down)
# => "12.3456%"
:significant

:precision 是否應套用至有效數字,而非小數位。預設為 false。

number_to_percentage(12345.6789)                                  # => "12345.679%"
number_to_percentage(12345.6789, significant: true)               # => "12300%"
number_to_percentage(12345.6789, precision: 2)                    # => "12345.68%"
number_to_percentage(12345.6789, precision: 2, significant: true) # => "12000%"
:separator

小數點分隔號。預設為(".")

:delimiter

千位分隔號。預設為(",")

:strip_insignificant_zeros

是否要移除小數點分隔號後不重要的零。預設為 false。

:format

輸出的格式。%n 代表數字。預設為 "%n%"

number_to_percentage(100, format: "%n  %")
# => "100.000  %"
# File activesupport/lib/active_support/number_helper.rb, line 223
def number_to_percentage(number, options = {})
  NumberToPercentageConverter.convert(number, options)
end

number_to_phone(number, options = {})

number 格式化為電話號碼。

number_to_phone(5551234)    # => "555-1234"
number_to_phone("5551234")  # => "555-1234"
number_to_phone(1235551234) # => "123-555-1234"
number_to_phone("12x34")    # => "12x34"

number_to_phone(1235551234, delimiter: ".", country_code: 1, extension: 1343)
# => "+1.123.555.1234 x 1343"

選項

:area_code

區域碼是否應使用括號括起來。預設為 false。

number_to_phone(1235551234, area_code: true)
# => "(123) 555-1234"
:delimiter

要使用的數位群組分隔符。預設為 "-"

number_to_phone(1235551234, delimiter: " ")
# => "123 555 1234"
:country_code

要加在前方的國家碼。

number_to_phone(1235551234, country_code: 1)
# => "+1-123-555-1234"
:extension

要加在後方的分機號碼。

number_to_phone(1235551234, extension: 555)
# => "123-555-1234 x 555"
:pattern

指定數位應如何分組的正規表示法。正規表示法的頭三個擷取結果會被視為數位群組。

number_to_phone(13312345678, pattern: /(\d{3})(\d{4})(\d{4})$/)
# => "133-1234-5678"
number_to_phone(75561234567, pattern: /(\d{1,4})(\d{4})(\d{4})$/, area_code: true)
# => "(755) 6123-4567"
# File activesupport/lib/active_support/number_helper.rb, line 88
def number_to_phone(number, options = {})
  NumberToPhoneConverter.convert(number, options)
end

number_to_rounded(number, options = {})

number 格式化為特定的精確度等級。

number_to_rounded(12345.6789)                # => "12345.679"
number_to_rounded(12345.6789, precision: 2)  # => "12345.68"
number_to_rounded(12345.6789, precision: 0)  # => "12345"
number_to_rounded(12345, precision: 5)       # => "12345.00000"

選項

:locale

用於格式化的區域。預設為當前區域。

number_to_rounded(111.234, locale: :fr)
# => "111,234"
:precision

精確度等級,或 nil 以保留 number 的精確度。預設為 3。

number_to_rounded(12345.6789, precision: nil)
# => "12345.6789"
:round_mode

指定進行捨入的方式。請參閱BigDecimal.mode。預設為:default

number_to_rounded(12.34, precision: 0, round_mode: :up)
# => "13"
:significant

:precision 是否應套用至有效數字,而非小數位。預設為 false。

number_to_rounded(12345.6789)                                  # => "12345.679"
number_to_rounded(12345.6789, significant: true)               # => "12300"
number_to_rounded(12345.6789, precision: 2)                    # => "12345.68"
number_to_rounded(12345.6789, precision: 2, significant: true) # => "12000"
:separator

小數點分隔號。預設為(".")

:delimiter

千位分隔號。預設為(",")

:strip_insignificant_zeros

是否要移除小數點分隔號後不重要的零。預設為 false。

number_to_rounded(12.34, strip_insignificant_zeros: false)  # => "12.340"
number_to_rounded(12.34, strip_insignificant_zeros: true)   # => "12.34"
number_to_rounded(12.3456, strip_insignificant_zeros: true) # => "12.346"
# File activesupport/lib/active_support/number_helper.rb, line 320
def number_to_rounded(number, options = {})
  NumberToRoundedConverter.convert(number, options)
end