Rounding Numbers With Precision
Ruby's Float#round
method gets the job done, but doesn't offer much configurability. If you'd like to finely control how a rounded number will display, ActiveSupport::NumberHelper
offers number_to_rounded
.
When a precision is specified, it will apply to the fraction digits:
Unless you include significant: true
in which case precision will refer to the number of signficant digits:
Because this is for display purposes, the return value is a string. You can further specify that insignificant zeros are stripped from the result:
And for completeness, here is an example of a number being rounded up:
Last updated