Class: Peanuts::Converter::Convert_float

Inherits:
Convert_string show all
Defined in:
lib/peanuts/converters.rb

Overview

A float.

Specifier

:float

Options

:precision

Floating point precision.

In addition accepts all options of Convert_string.

Instance Method Summary collapse

Methods inherited from Peanuts::Converter

create, create!, lookup, lookup!

Constructor Details

#initialize(options) ⇒ Convert_float

Returns a new instance of Convert_float.



173
174
175
176
177
# File 'lib/peanuts/converters.rb', line 173

def initialize(options)
  super
  @precision = options[:precision]
  @format = @precision ? "%f.#{@precision}" : '%f'
end

Instance Method Details

#from_xml(string) ⇒ Object



183
184
185
# File 'lib/peanuts/converters.rb', line 183

def from_xml(string)
  (string = super(string)) && string.to_f
end

#to_xml(int) ⇒ Object



179
180
181
# File 'lib/peanuts/converters.rb', line 179

def to_xml(int)
  super(int && sprintf(@format, int))
end