This project is archived and is in readonly mode.
Rounding problem in number_with_precision
-
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)
-
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 tosprintf.Existing tests for
number_with_precisionstill work as expected. -
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
-
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),
