| 3 | | class BigDecimal #:nodoc: |
|---|
| 4 | | alias :_original_to_s :to_s |
|---|
| 5 | | def to_s(format="F") |
|---|
| 6 | | _original_to_s(format) |
|---|
| 7 | | end |
|---|
| 8 | | |
|---|
| 9 | | yaml_as "tag:yaml.org,2002:float" |
|---|
| 10 | | def to_yaml( opts = {} ) |
|---|
| 11 | | YAML::quick_emit( nil, opts ) do |out| |
|---|
| 12 | | # This emits the number without any scientific notation. |
|---|
| 13 | | # I prefer it to using self.to_f.to_s, which would lose precision. |
|---|
| 14 | | # |
|---|
| 15 | | # Note that YAML allows that when reconsituting floats |
|---|
| 16 | | # to native types, some precision may get lost. |
|---|
| 17 | | # There is no full precision real YAML tag that I am aware of. |
|---|
| 18 | | str = self.to_s |
|---|
| 19 | | if str == "Infinity" |
|---|
| 20 | | str = ".Inf" |
|---|
| 21 | | elsif str == "-Infinity" |
|---|
| 22 | | str = "-.Inf" |
|---|
| 23 | | elsif str == "NaN" |
|---|
| 24 | | str = ".NaN" |
|---|
| | 3 | module ActiveSupport #:nodoc: |
|---|
| | 4 | module CoreExtensions #:nodoc: |
|---|
| | 5 | module BigDecimal #:nodoc: |
|---|
| | 6 | module Conversions |
|---|
| | 7 | def self.included(base) #:nodoc: |
|---|
| | 8 | base.instance_eval do |
|---|
| | 9 | alias_method :_original_to_s, :to_s |
|---|
| | 10 | alias_method :to_s, :to_formatted_s |
|---|
| | 11 | end |
|---|
| | 12 | end |
|---|
| | 13 | |
|---|
| | 14 | def to_formatted_s(format="F") |
|---|
| | 15 | _original_to_s(format) |
|---|
| | 16 | end |
|---|
| | 17 | |
|---|
| | 18 | yaml_as "tag:yaml.org,2002:float" |
|---|
| | 19 | def to_yaml( opts = {} ) |
|---|
| | 20 | YAML::quick_emit( nil, opts ) do |out| |
|---|
| | 21 | # This emits the number without any scientific notation. |
|---|
| | 22 | # I prefer it to using self.to_f.to_s, which would lose precision. |
|---|
| | 23 | # |
|---|
| | 24 | # Note that YAML allows that when reconsituting floats |
|---|
| | 25 | # to native types, some precision may get lost. |
|---|
| | 26 | # There is no full precision real YAML tag that I am aware of. |
|---|
| | 27 | str = self.to_s |
|---|
| | 28 | if str == "Infinity" |
|---|
| | 29 | str = ".Inf" |
|---|
| | 30 | elsif str == "-Infinity" |
|---|
| | 31 | str = "-.Inf" |
|---|
| | 32 | elsif str == "NaN" |
|---|
| | 33 | str = ".NaN" |
|---|
| | 34 | end |
|---|
| | 35 | out.scalar( "tag:yaml.org,2002:float", str, :plain ) |
|---|
| | 36 | end |
|---|
| | 37 | end |
|---|