joshwlewis / unitwise

Physical quantity and units of measure conversion and math for Ruby
unitwise.org
MIT License
282 stars 32 forks source link

Cannot convert to decimal `#to_d` #59

Open amingilani opened 6 years ago

amingilani commented 6 years ago

The library currently supports converting to integers and floats, but for precise measurements, you should use decimal and currently converting directly doesn't work.

Example of current behavior:

distance = Unitwise(0.25, 'mile')   # => #<Unitwise::Measurement value=0.25 unit=mile>
distance.to_s # => "0.25 mile"
distance.to_i # => 0
distance.to_f # => 0.25
distance.to_d # => Unitwise::ConversionError: Can't convert 0.25 mile to d.
distance.to_f.to_d # => 0.25e0

Expected behavior:

distance.to_d => 0.25e0