Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

Rounding problem in number_with_precision

#1340

Reported by Ryan Warnick · November 6th, 2008 @ 10:00 PM

State: resolved
Milestone: 2.x
Assigned to: Jeremy Kemper Jeremy Kemper
Importance: none

Activity

  1. Ryan Warnick
    Ryan Warnick
    • 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)

    November 6th, 2008 @ 10:07 PM

  2. C. Bedard
    C. Bedard
    • Tag set to number_helper, number_with_precision, patch, round

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

    Ticket #2071 [VERIFIED] number_to_precision rounding error 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.

    February 27th, 2009 @ 03:06 PM

  3. Pratik
    Pratik
    • Assigned user set to Jeremy Kemper
    • Tag changed from number_helper, number_with_precision, patch, round to number_helper, number_with_precision, patch, round

    June 21st, 2009 @ 05:41 PM

  4. Walter Horstman
    Walter Horstman

    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),

    January 11th, 2010 @ 04:11 PM

  5. José Valim
    José Valim
    • State changed from new to resolved

    Fixed on master.

    March 19th, 2010 @ 10:14 PM