This project is archived and is in readonly mode.

#6182 ✓resolved
Don Wilson

Another rounding problem in number_with_precision

Reported by Don Wilson | December 16th, 2010 @ 08:01 PM | in 3.0.5

Currently, if you run number_with_precision(9.995, :precision => 2) it returns "9.99". It should return "10.00". You can read a little more information here:

It basically comes down to this:

# File actionpack/lib/action_view/helpers/number_helper.rb, line 280
rounded_number = * (10 ** precision)).to_s).round.to_f / 10 ** precision

You can see here that the number is multiplied by 100 before it is converted to BigDecimal. When working with Floats, 9.995 * 100 = 999.4999999999999 which will obviously round down to 999, not up to 1000.

My Fix

I have attached a patch for this that just uses the BigDecimal round function which does this perfectly. In the case where significant is true, it just converts the number to BigDecimal before doing its rounding, etc.

Let me know what you think!

Comments and changes to this ticket

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=""></a>

Referenced by