Permalink
Browse files

Merge pull request #5609 from tjmcewan/master

prevent returning a negative zero from number_with_precision
  • Loading branch information...
2 parents ded74df + 7051a23 commit 19901c6c61d1900a1452c8b14943a8345463f039 @josevalim josevalim committed Mar 27, 2012
@@ -292,6 +292,7 @@ def number_with_precision(number, options = {})
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 = rounded_number.zero? ? rounded_number.abs : rounded_number #prevent showing negative zeros
end
formatted_number = number_with_delimiter("%01.#{precision}f" % rounded_number, options)
if strip_insignificant_zeros
@@ -96,6 +96,7 @@ def test_number_with_precision
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("0.00", number_with_precision(-0.001, :precision => 2))
end
def test_number_with_precision_with_custom_delimiter_and_separator

0 comments on commit 19901c6

Please sign in to comment.