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

Ticket #8027 (closed defect: fixed)

Opened 1 year ago

Last modified 6 months ago

number_to_currency rounding error.

Reported by: impulsedev Assigned to: core
Priority: normal Milestone: 2.x
Component: ActionPack Version: edge
Severity: normal Keywords:
Cc:

Description

Hi,

While building a shopping cart for a client in Texas (where the sales tax rate is 8.25%), I noticed a rounding error when executing number_to_currency.

number_to_currency(0.825) will output $0.83, which is correct. number_to_currency(10.825) will output $10.82, which is incorrect.

This "rounding down" occurs when rounding 10.825, 11.825, 12.825... all the way up to 31.825. 32.825 will round up to 32.83, which is correct.

I viewed the source code for number_to_currency and number_with_precision, but I couldn't find an error. Not sure what is wrong.

Brandon

Attachments

number_to_currency_test.rhtml (3.1 kB) - added by jesperronn on 05/05/07 17:21:26.
I'm adding a view file to show this weird error. Just add to an existing Rails app.
number_to_currency_test.rb (0.8 kB) - added by jesperronn on 05/05/07 17:45:02.
ruby version that shows the error
added_tests_to_test_number_with_precision.diff (0.7 kB) - added by lau on 05/05/07 18:22:29.
Adds asserts to the unit test of test_number_with_precision that fails under at least some compilations of ruby on OSX and a version of cygwin on Windows.

Change History

05/05/07 17:21:26 changed by jesperronn

  • attachment number_to_currency_test.rhtml added.

I'm adding a view file to show this weird error. Just add to an existing Rails app.

05/05/07 17:45:02 changed by jesperronn

  • attachment number_to_currency_test.rb added.

ruby version that shows the error

05/05/07 17:49:52 changed by jesperronn

This file is still in ruby. So the error is not number_to_currency, but in the call that is made by number_to_precision:

  "%01.#{precision}f" % number

Any ideas for a patch to number_to_precision so that it does not use this flawed ruby call??

05/05/07 18:22:29 changed by lau

  • attachment added_tests_to_test_number_with_precision.diff added.

Adds asserts to the unit test of test_number_with_precision that fails under at least some compilations of ruby on OSX and a version of cygwin on Windows.

05/05/07 20:25:36 changed by lau

The unit tests revealing the sprintf bug in Ruby have been added to a new ticket: #8275

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]