Class: RDF::Literal::Double

Inherits:
Object
  • Object
show all
Defined in:
lib/json/ld/extensions.rb

Instance Method Summary collapse

Instance Method Details

#canonicalize!Object

Converts this literal into its canonical lexical representation. Update to use %.15E to avoid precision problems



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/json/ld/extensions.rb', line 46

def canonicalize!
  @string = case
    when @object.nan?      then 'NaN'
    when @object.infinite? then @object.to_s[0...-'inity'.length].upcase
    when @object.zero?     then '0.0E0'
    else
      i, f, e = ('%.15E' % @object.to_f).split(/[\.E]/)
      f.sub!(/0*$/, '')           # remove any trailing zeroes
      f = '0' if f.empty?         # ...but there must be a digit to the right of the decimal point
      e.sub!(/^\+?0+(\d)$/, '\1') # remove the optional leading '+' sign and any extra leading zeroes
      "#{i}.#{f}E#{e}"
  end
  @object = Float(@string) unless @object.nil?
  self
end