ActionView::Helpers::NumberHelper
provides methods
for converting numbers into formatted strings.
Example:
As shown in the above example, number to currency conversion with precision 0 resulted in rounding down the number. Sometimes, we need to round up such numbers to nearest integer than to round down. We lost $0.45 in the above example if we go by regular rounding logic.
To handle such scenarios,
Rails has added
round_mode
parameter to number helpers.
round_mode
accepts value the same as mode in Ruby BigDecimal::ROUND_MODE
.
BigDecimal
provides built-in support
for arbitrary precision on very large
or very accurate floating-point numbers in Ruby
The accepted values along with examples are:
:up
: Round away from zero
:down
,:truncate
: Round towards zero (truncate)
:half_up
,:default
: Round towards the nearest neighbor, unless both neighbors are equidistant, in which case round away from zero. This is the default.
:half_down
: Round towards the nearest neighbor, unless both neighbors are equidistant, in which case round towards zero.
:half_even
,:banker
: Round towards the nearest neighbor, unless both neighbors are equidistant, in which case round towards the even neighbor.
:ceiling
,:ceil
: Round towards positive infinity (ceil)
:floor
: Round towards negative infinity (floor)