This project is archived and is in readonly mode.

#1340 ✓resolved
Ryan Warnick

Rounding problem in number_with_precision

Reported by Ryan Warnick | November 6th, 2008 @ 10:00 PM | in 2.x

Comments and changes to this ticket

  • Ryan Warnick

    Ryan Warnick November 6th, 2008 @ 10:07 PM

    • Title changed from “Rounding problem in number_with” to “Rounding problem in number_with_precision”

    I know that this has been a ticket before (see Rails Trac #8027), but the fix that resolved that ticket doesn't seem to work in every case. Try formatting 17.935 to 2 decimal places. This works on some platforms, but not on others. All of our Red Hat Linux servers return 17.93. This problem probably needs to be addressed on the Ruby side more than anything else, but in the mean time, maybe we can do something in Rails for the short term. Here is something that worked (as far as we've tested) for us.

    Change

    "%01.#{precision}f" % ((Float(number) * (10 precision)).round.to_f / 10 precision)

    TO

    "%01.#{precision}f" % ((Float(number) * (10 precision)).to_d.round.to_f / 10 precision)

  • C. Bedard

    C. Bedard February 27th, 2009 @ 03:06 PM

    • Tag set to number_helper, number_with_precision, patch, round

    As Ryan Warnick mentions, it really is a Ruby issue with sprintf.

    Ticket #2071 seems to be a duplicate of this report.

    Attached is a patch to fix the problem consistently.

    This patch uses BigDecimal#round(precision) to round the number before passing it to sprintf.

    Existing tests for number_with_precision still work as expected.

  • Pratik

    Pratik June 21st, 2009 @ 05:41 PM

    • Assigned user set to “Jeremy Kemper”
    • Tag changed from number_helper, number_with_precision, patch, round to number_helper, number_with_precision, patch, round
  • Walter Horstman

    Walter Horstman January 11th, 2010 @ 04:11 PM

    What's the status of this bug? To me it seems the patch mentioned isn't working: in case the given number has less decimal digits then the precision given, it returns too little precision digits. And in case of a precision 0, it still returns one precision digit. I think the line with the BigDecimal in it, should be:

    number_with_delimiter("%01.#{precision}f" % BigDecimal.new(number.to_s).round(precision),

  • José Valim

    José Valim March 19th, 2010 @ 10:14 PM

    • State changed from “new” to “resolved”

    Fixed on master.

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>

Referenced by

Pages