for converting numbers into formatted strings.
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 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
:truncate: Round towards zero (truncate)
: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.
:banker: Round towards the nearest neighbor, unless both neighbors are equidistant, in which case round towards the even neighbor.
:ceil: Round towards positive infinity (ceil)
:floor: Round towards negative infinity (floor)