From c7d3bd48dff0a509f5c21ec8864cb4f774d604e6 Mon Sep 17 00:00:00 2001
From: Sean Griffin <sean@seantheprogrammer.com>
Date: Thu, 24 Mar 2016 16:06:40 -0600
Subject: Apply scale before precision when coercing floats to decimal

Since precision is always larger than scale, it can actually change
rounding behavior. Given a precision of 5 and a scale of 3, when you
apply the precision of 5 to `1.25047`, the result is `1.2505`, which
when the scale is applied would be `1.251` instead of the expected
`1.250`.

This issue appears to only occur with floats, as scale doesn't apply to
other numeric types, and the bigdecimal constructor actually ignores
precision entirely when working with strings. There's no way we could
handle this for the "unknown object which responds to `to_d`" case, as
we can't assume an interface for applying the scale.

Fixes #24235
---
 activemodel/test/cases/type/decimal_test.rb | 7 +++++++
 1 file changed, 7 insertions(+)

(limited to 'activemodel/test')

diff --git a/activemodel/test/cases/type/decimal_test.rb b/activemodel/test/cases/type/decimal_test.rb
index 353dbf84ad..1950566c0e 100644
--- a/activemodel/test/cases/type/decimal_test.rb
+++ b/activemodel/test/cases/type/decimal_test.rb
@@ -52,6 +52,13 @@ module ActiveModel
         assert_not type.changed?(5.0, 5.0, '5.0')
         assert_not type.changed?(-5.0, -5.0, '-5.0')
       end
+
+      def test_scale_is_applied_before_precision_to_prevent_rounding_errors
+        type = Decimal.new(precision: 5, scale: 3)
+
+        assert_equal BigDecimal("1.250"), type.cast(1.250473853637869)
+        assert_equal BigDecimal("1.250"), type.cast("1.250473853637869")
+      end
     end
   end
 end
-- 
cgit v1.2.3