diff options
author | Kir Shatrov <shatrov@me.com> | 2017-01-22 10:28:01 -0500 |
---|---|---|
committer | Kir Shatrov <shatrov@me.com> | 2017-01-22 10:28:01 -0500 |
commit | 7ec30400e12d547ab573281c442ccb8a48d80495 (patch) | |
tree | 75d2b39e5a2207070cf3e2f4af472e1c5a72339f /activemodel/lib | |
parent | 20107ff6c80a7e162adf22176eee811a59498126 (diff) | |
download | rails-7ec30400e12d547ab573281c442ccb8a48d80495.tar.gz rails-7ec30400e12d547ab573281c442ccb8a48d80495.tar.bz2 rails-7ec30400e12d547ab573281c442ccb8a48d80495.zip |
Make BigDecimal casting consistent on different platforms
Right now it behaves differently on JRuby:
```
--- expected
+++ actual
@@ -1 +1 @@
-#<BigDecimal:5f3c866c,'0.333333333333333333',18(20)>
+#<BigDecimal:16e0afab,'0.3333333333333333',16(20)>
```
My initial PR (https://github.com/rails/rails/pull/27324)
offered to let the precision to be decided by the platform and
change the test expection, but other contributors suggested
that we should change the default precision in Rails
to be consistent of all platforms.
The value (18) comes from the max default precision that comes
from casting Rational(1/3) to BigDecimal.
Diffstat (limited to 'activemodel/lib')
-rw-r--r-- | activemodel/lib/active_model/type/decimal.rb | 3 |
1 files changed, 2 insertions, 1 deletions
diff --git a/activemodel/lib/active_model/type/decimal.rb b/activemodel/lib/active_model/type/decimal.rb index 6c5c0451c6..541a12c8a1 100644 --- a/activemodel/lib/active_model/type/decimal.rb +++ b/activemodel/lib/active_model/type/decimal.rb @@ -4,6 +4,7 @@ module ActiveModel module Type class Decimal < Value # :nodoc: include Helpers::Numeric + BIGDECIMAL_PRECISION = 18 def type :decimal @@ -21,7 +22,7 @@ module ActiveModel when ::Float convert_float_to_big_decimal(value) when ::Numeric, ::String - BigDecimal(value, precision.to_i) + BigDecimal(value, precision || BIGDECIMAL_PRECISION) else if value.respond_to?(:to_d) value.to_d |