Permalink
Browse files

Revert "Fixed number_with_precision rounding error [#6182 state:resol…

…ved]"

This reverts commit 006ec77.
  • Loading branch information...
1 parent f7e3362 commit 07567aea5f0c6f14a53f511ab4cc46201b669795 @drogus drogus committed Dec 17, 2010
@@ -270,13 +270,12 @@ def number_with_precision(number, options = {})
digits, rounded_number = 1, 0
else
digits = (Math.log10(number.abs) + 1).floor
- rounded_number = (BigDecimal.new(number.to_s) / 10 ** (digits - precision)).round.to_f * 10 ** (digits - precision)
- digits = (Math.log10(rounded_number.abs) + 1).floor # After rounding, the number of digits may have changed
+ rounded_number = BigDecimal.new((number / 10 ** (digits - precision)).to_s).round.to_f * 10 ** (digits - precision)
end
precision -= digits
precision = precision > 0 ? precision : 0 #don't let it be negative
else
- rounded_number = BigDecimal.new(number.to_s).round(precision).to_f
+ rounded_number = BigDecimal.new((number * (10 ** precision)).to_s).round.to_f / 10 ** precision
end
formatted_number = number_with_delimiter("%01.#{precision}f" % rounded_number, options)
if strip_insignificant_zeros
@@ -100,11 +100,6 @@ def test_number_with_precision
assert_equal("0", number_with_precision(0, :precision => 0))
assert_equal("0.00100", number_with_precision(0.001, :precision => 5))
assert_equal("0.001", number_with_precision(0.00111, :precision => 3))
- assert_equal("10.00", number_with_precision(9.995, :precision => 2))
- assert_equal("11.00", number_with_precision(10.995, :precision => 2))
- assert_equal("10.0", number_with_precision(9.995, :precision => 3, :significant => true))
- assert_equal("9.99", number_with_precision(9.994, :precision => 3, :significant => true))
- assert_equal("11.0", number_with_precision(10.995, :precision => 3, :significant => true))
end
def test_number_with_precision_with_custom_delimiter_and_separator

0 comments on commit 07567ae

Please sign in to comment.