require 'test/unit' require 'test/framework' require 'units' class TC_formatting < Test::Unit::TestCase understands UnitsTest def test_fractions Units.create :length do |m| m.system :english do |s| s.unit :name => :inch, :plural => :inches, :abbrev => :in s.unit :name => :foot, :plural => :feet, :abbrev => :ft, :equals => 12.inches s.unit :name => :yard, :abbrevs => :yd, :equals => 3.feet s.unit :name => :mile, :abbrevs => :mi, :equals => 1760.yards s.format :name => :feet_inches_and_32s, :format => lambda { |u| ru = u.inch.round_to_nearest 32 feet = ru.feet.floor inches = (ru-feet).inches "#{feet} #{inches.with_fraction 32}" } end end l = 13.28090635.ft assert(l.measure == Units.length) assert(3.46875.with_fraction(32) == "3-15/32") assert(l.format(:feet_inches_and_32s) == "13 feet 3-12/32 inches") almost = 1.999.ft assert(almost.inch.round_to_nearest(32) == 24.0.in) almost = 1.996.ft assert(almost.inch.round_to_nearest(32) == 23.9375.in) end end