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

Ticket #10090 (closed defect: fixed)

Opened 10 months ago

Last modified 5 months ago

number_with_precision documentation

Reported by: nigel.bris Assigned to: core
Priority: low Milestone: 2.x
Component: ActionPack Version: edge
Severity: minor Keywords:
Cc:

Description

In the number_with_precision documentation it has the following example:-

number_with_precision(111.2345, 2) => 111.24

the result should be 111.23

Ps. the method does return the correct result, just the documentation has a typo.

Change History

11/07/07 10:06:45 changed by Henrik N

Actually not just that example, but the first two examples are wrong, in relation to what the command does.

Examples in documentation:

#  number_with_precision(111.2345)     # => 111.235
#  number_with_precision(111.2345, 2)  # => 111.24
#  number_with_precision(13, 5)        # => 13.00000
#  number_with_precision(389.32314, 0) # => 389

In script/console:

Loading development environment (Rails 1.2.5)
>> include ActionView::Helpers::NumberHelper
=> Object
>> number_with_precision(111.2345)
=> "111.234"
>> number_with_precision(111.2345, 2)
=> "111.23"

The documentation assumes that .5 is rounded up, but number_with_precision actually seems to round-to-even:

>> number_with_precision(3.015, 2)
=> "3.02"
>> number_with_precision(3.045, 2)
=> "3.04"
>> 

So either the documentation should be fixed (both to give the correct results and to mention the perhaps unexpected rounding method) or the code should be fixed (to always round .5 up).

11/07/07 10:07:34 changed by Henrik N

  • type changed from enhancement to defect.
  • component changed from Web to ActionPack.

11/07/07 10:34:06 changed by nigel.bris

Interesting, from console on:- rails: 1.2.3 gem: 0.9.2 ruby: 1.8.6

include ActionView::Helpers::NumberHelper

=> Object

number_with_precision(111.2345)

=> "111.235"

We get the correct result.

11/07/07 10:42:57 changed by Henrik N

My results were with

  • OS X Leopard
  • ruby 1.8.6 (2007-06-07 patchlevel 36) [universal-darwin9.0] (bundled with Leopard)
  • Edge Rails, revision 8106

03/24/08 21:12:59 changed by david

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

(In [9086]) Fixed NumberHelper#number_with_precision to properly round in a way that works equally on Mac, Windows, Linux (closes #11409, #8275, #10090, #8027) [zhangyuanyi]