number_with_delimiter(number, options = {}) public

Formats a number with grouped thousands using delimiter (e.g., 12,324). You can customize the format in the options hash.

Options

  • :locale - Sets the locale to be used for formatting (defaults to current locale).

  • :delimiter - Sets the thousands delimiter (defaults to “,”).

  • :separator - Sets the separator between the fractional and integer digits (defaults to “.”).

  • :delimiter_pattern - Sets a custom regular expression used for deriving the placement of delimiter. Helpful when using currency formats like INR.

  • :raise - If true, raises InvalidNumberError when the argument is invalid.

Examples

 number_with_delimiter(12345678)                        # => 12,345,678
 number_with_delimiter("123456")                        # => 123,456
 number_with_delimiter(12345678.05)                     # => 12,345,678.05
 number_with_delimiter(12345678, delimiter: ".")        # => 12.345.678
 number_with_delimiter(12345678, delimiter: ",")        # => 12,345,678
 number_with_delimiter(12345678.05, separator: " ")     # => 12,345,678 05
 number_with_delimiter(12345678.05, locale: :fr)        # => 12 345 678,05
 number_with_delimiter("112a")                          # => 112a
 number_with_delimiter(98765432.98, delimiter: " ", separator: ",")
 # => 98 765 432,98

 number_with_delimiter("123456.78",
   delimiter_pattern: /(\d+?)(?=(\d\d)+(\d)(?!\d))/)    # => "1,23,456.78"

number_with_delimiter("112a", raise: true)              # => raise InvalidNumberError
Show source
Register or log in to add new notes.
October 25, 2008
2 thanks

Alternative: use 1000.humanize

1.humanize == “1″ 1000000.humanize == “1.000.000″ 1000.12345.humanize == “1.000,12″

http://pragmatig.wordpress.com/2008/10/25/numbers-for-humans-humanize-for-numeric/

April 9, 2009
0 thanks

Deprecation warning for old-style options

You will get a warning if you don’t define your separators as a hash:

DEPRECATION WARNING: number_with_delimiter takes an option hash instead of separate delimiter and precision arguments

So while you can still use that style, it’s not without a scolding.