Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Ticket #8626 (closed enhancement: fixed)

Opened 1 year ago

Last modified 1 year ago

[PATCH] Float#round with precision

Reported by: norbert Assigned to: core
Priority: normal Milestone: 1.x
Component: ActiveSupport Version: edge
Severity: normal Keywords: core_ext tiny verified
Cc:

Description

Allow floats to be rounded with a specified precision.

x = 1.337
x.round    # => 1
x.round(1) # => 1.3
x.round(2) # => 1.34

Attachments

round_with_precision.diff (1.4 kB) - added by norbert on 06/11/07 15:42:31.
round_with_precision.2.diff (1.7 kB) - added by norbert on 06/13/07 15:11:13.
round_with_precision.3.diff (2.4 kB) - added by norbert on 08/14/07 15:42:38.

Change History

06/11/07 15:42:31 changed by norbert

  • attachment round_with_precision.diff added.

06/11/07 15:44:32 changed by norbert

  • summary changed from Float#round with precision to [PATCH] Float#round with precision.

06/13/07 15:11:13 changed by norbert

  • attachment round_with_precision.2.diff added.

06/13/07 15:13:55 changed by norbert

The new patch is way more dense and adds some test cases, as suggested by koppen in #rails-contrib.

07/19/07 11:52:16 changed by alloy

Rounding with a non-integer argument seems unneeded. I find it's the developers responsibility to provide an integer, thus not needing the extra to_i in cases where an integer is being passed.

precision.nil? ? round_without_precision : (self*(10**precision)).round/(10**precision).to_f

Otherwise a very nice feature. +1

07/19/07 12:30:50 changed by manfred

Norbert and I discussed the patch IRL, +1

08/14/07 15:42:38 changed by norbert

  • attachment round_with_precision.3.diff added.

08/14/07 15:53:07 changed by norbert

New version, now with the core ext in a separate module and including the snippet by alloy. The code is cleaner and it allows negative precisions, like so:

123456.0.round(-2) # => 123500.0

08/21/07 10:49:41 changed by alloy

Should be good to go IMO. +1

08/21/07 18:13:28 changed by tarmo

  • keywords changed from core_ext tiny to core_ext tiny verified.

Looks good, +1

09/02/07 23:56:48 changed by nzkoz

  • status changed from new to closed.
  • resolution set to fixed.

(In [7401]) Allow Float#round to take a precision argument. Closes #8626 [norbert]