3 module ActiveSupport
#:nodoc:
4 module CoreExtensions
#:nodoc:
5 module BigDecimal
#:nodoc:
7 DEFAULT_STRING_FORMAT
= 'F'.freeze
8 YAML_TAG
= 'tag:yaml.org,2002:float'.freeze
9 YAML_MAPPING
= { 'Infinity' => '.Inf', '-Infinity' => '-.Inf', 'NaN' => '.NaN' }
11 def self.included(base
) #:nodoc:
13 alias_method
:_original_to_s, :to_s
14 alias_method
:to_s, :to_formatted_s
20 def to_formatted_s(format
= DEFAULT_STRING_FORMAT
)
21 _original_to_s(format
)
24 # This emits the number without any scientific notation.
25 # This is better than self.to_f.to_s since it doesn't lose precision.
27 # Note that reconstituting YAML floats to native floats may lose precision.
28 def to_yaml(opts
= {})
29 YAML
.quick_emit(nil, opts
) do |out
|
31 out
.scalar(YAML_TAG
, YAML_MAPPING
[string
] || string
, :plain)