Hanami offers a helpful way to present numbers via #format_number, a private method available only in views.

Usage

  1. module Web
  2. module Views
  3. module Books
  4. class Show
  5. include Web::View
  6. def download_count
  7. format_number book.download_count
  8. end
  9. end
  10. end
  11. end
  12. end
  1. <span><%= download_count %></span>
  1. <span>1,000,000</span>

Precision

The default precision is of 2, but we can specify a different value with the homonym option.

  1. format_number(Math::PI) # => "3.14"
  2. format_number(Math::PI, precision: 6) # => "3.141592"

Delimiter

The default thousands delimiter is ,. We can use :delimiter for a different char.

  1. format_number(1_000_000) # => "1,000,000"
  2. format_number(1_000_000, delimiter: '.') # => "1.000.000"

Separator

The default separator is .. We can use :separator for a different char.

  1. format_number(1.23) # => "1.23"
  2. format_number(1.23, separator: ',') # => "1,23"