This project is archived and is in readonly mode.
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 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 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 tosprintf
.Existing tests for
number_with_precision
still work as expected. -
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 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),
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>
People watching this ticket
Attachments
Referenced by
- 4239 [PATCH] Improvements in number_helper.rb BTW, somebody please close #1340 as it looks like it was ...