Ticket #8746: bigdecimal2.diff
| File bigdecimal2.diff, 2.1 kB (added by vesaria, 1 year ago) |
|---|
-
activesupport/test/bigdecimal_test.rb
old new 1 require File.dirname(__FILE__) + '/abstract_unit' 2 require 'active_support/bigdecimal' 3 4 class BigDecimalYamlTest < Test::Unit::TestCase 5 def test_to_yaml 6 assert_equal "--- 100000.30020320320000000000000000000000000000001\n", BigDecimal.new('100000.30020320320000000000000000000000000000001').to_yaml 7 assert_equal "--- .Inf\n", BigDecimal.new('Infinity').to_yaml 8 assert_equal "--- .NaN\n", BigDecimal.new('NaN').to_yaml 9 end 10 end -
activesupport/lib/active_support/bigdecimal.rb
old new 1 require 'test/unit' 2 require 'yaml' 3 require 'bigdecimal' 4 require 'rubygems' 5 require 'active_support' 6 7 class BigDecimal 8 yaml_as "tag:yaml.org,2002:float" 9 def to_yaml( opts = {} ) 10 YAML::quick_emit( nil, opts ) do |out| 11 # This emits the number without any scientific notation. 12 # I prefer it to using self.to_f.to_s, which would lose precision. 13 # 14 # Note that YAML allows that when reconsituting floats 15 # to native types, some precision may get lost. 16 # There is no full precision real YAML tag that I am aware of. 17 str = self.to_s 18 if str == "Infinity" 19 str = ".Inf" 20 elsif str == "-Infinity" 21 str = "-.Inf" 22 elsif str == "NaN" 23 str = ".NaN" 24 end 25 out.scalar( "tag:yaml.org,2002:float", str, :plain ) 26 end 27 end 28 end -
activesupport/lib/active_support.rb
old new 42 42 require 'active_support/json' 43 43 44 44 require 'active_support/multibyte' 45 46 require 'active_support/bigdecimal'