1
0
mirror of https://github.com/jbranchaud/til synced 2026-01-03 15:18:01 +00:00
Files
til/rails/rounding-numbers-with-precision.md
2019-11-22 12:46:51 -06:00

1.3 KiB

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:

> ActiveSupport::NumberHelper.number_to_rounded(1.0, precision: 2)
=> "1.00"

Unless you include significant: true in which case precision will refer to the number of signficant digits:

> ActiveSupport::NumberHelper.number_to_rounded(1.0, precision: 2, significant: true)
=> "1.0"

Because this is for display purposes, the return value is a string. You can further specify that insignificant zeros are stripped from the result:

> ActiveSupport::NumberHelper.number_to_rounded(1.0, precision: 2, significant: true, strip_insignificant_zeros: true)
=> "1"

And for completeness, here is an example of a number being rounded up:

> ActiveSupport::NumberHelper.number_to_rounded(1.29, precision: 2, significant: true)
=> "1.3"

source