# encoding: UTF-8 require 'helper' class TestModalh < Test::Unit::TestCase should "group digits with a delimiter" do assert_equal "123.456", H.to(123.456, :delimiter=>',') assert_equal "123", H.to(123.0, :delimiter=>',') assert_equal "123", H.to(123, :delimiter=>',') assert_equal "12.456", H.to(12.456, :delimiter=>',') assert_equal "1.456", H.to(1.456, :delimiter=>',') assert_equal "1", H.to(1, :delimiter=>',') assert_equal "1", H.to(1.0, :delimiter=>',') assert_equal "1,234.567", H.to(1234.567, :delimiter=>',') assert_equal "41,234.567", H.to(41234.567, :delimiter=>',') assert_equal "241,234.567", H.to(241234.567, :delimiter=>',') assert_equal "3,241,234.567", H.to(3241234.567, :delimiter=>',') assert_equal "3,241,234", H.to(3241234.0, :delimiter=>',') assert_equal "3,241,234", H.to(3241234, :delimiter=>',') assert_equal "-123.456", H.to(-123.456, :delimiter=>',') assert_equal "-123", H.to(-123.0, :delimiter=>',') assert_equal "-123", H.to(-123, :delimiter=>',') assert_equal "-12.456", H.to(-12.456, :delimiter=>',') assert_equal "-1.456", H.to(-1.456, :delimiter=>',') assert_equal "-1", H.to(-1, :delimiter=>',') assert_equal "-1", H.to(-1.0, :delimiter=>',') assert_equal "-1,234.567", H.to(-1234.567, :delimiter=>',') assert_equal "-41,234.567", H.to(-41234.567, :delimiter=>',') assert_equal "-241,234.567", H.to(-241234.567, :delimiter=>',') assert_equal "-3,241,234.567", H.to(-3241234.567, :delimiter=>',') assert_equal "-3,241,234", H.to(-3241234.0, :delimiter=>',') assert_equal "-3,241,234", H.to(-3241234, :delimiter=>',') end should "group digits with a delimiter and round" do assert_equal "123.46", H.to(123.456, :delimiter=>',', :precision=>2) assert_equal "123.00", H.to(123.0, :delimiter=>',', :precision=>2) assert_equal "123.00", H.to(123, :delimiter=>',', :precision=>2) assert_equal "12.46", H.to(12.456, :delimiter=>',', :precision=>2) assert_equal "1.46", H.to(1.456, :delimiter=>',', :precision=>2) assert_equal "1.00", H.to(1, :delimiter=>',', :precision=>2) assert_equal "1.00", H.to(1.0, :delimiter=>',', :precision=>2) assert_equal "1,234.57", H.to(1234.567, :delimiter=>',', :precision=>2) assert_equal "41,234.57", H.to(41234.567, :delimiter=>',', :precision=>2) assert_equal "241,234.57", H.to(241234.567, :delimiter=>',', :precision=>2) assert_equal "3,241,234.57", H.to(3241234.567, :delimiter=>',', :precision=>2) assert_equal "3,241,234.00", H.to(3241234.0, :delimiter=>',', :precision=>2) assert_equal "3,241,234.00", H.to(3241234, :delimiter=>',', :precision=>2) assert_equal "-123.46", H.to(-123.456, :delimiter=>',', :precision=>2) assert_equal "-123.00", H.to(-123.0, :delimiter=>',', :precision=>2) assert_equal "-123.00", H.to(-123, :delimiter=>',', :precision=>2) assert_equal "-12.46", H.to(-12.456, :delimiter=>',', :precision=>2) assert_equal "-1.46", H.to(-1.456, :delimiter=>',', :precision=>2) assert_equal "-1.00", H.to(-1, :delimiter=>',', :precision=>2) assert_equal "-1.00", H.to(-1.0, :delimiter=>',', :precision=>2) assert_equal "-1,234.57", H.to(-1234.567, :delimiter=>',', :precision=>2) assert_equal "-41,234.57", H.to(-41234.567, :delimiter=>',', :precision=>2) assert_equal "-241,234.57", H.to(-241234.567, :delimiter=>',', :precision=>2) assert_equal "-3,241,234.57", H.to(-3241234.567, :delimiter=>',', :precision=>2) assert_equal "-3,241,234.00", H.to(-3241234.0, :delimiter=>',', :precision=>2) assert_equal "-3,241,234.00", H.to(-3241234, :delimiter=>',', :precision=>2) assert_equal "123.400", H.to(123.4, :delimiter=>',', :precision=>3) end should "parse numbers with delimiters" do assert_equal 123.456, H.from("123.456", :type=>Float, :delimiter=>',') assert_equal 123.0, H.from("123", :type=>Float, :delimiter=>',') assert_equal 123.0, H.from("123.0", :type=>Float, :delimiter=>',') assert_equal 123, H.from("123", :type=>Integer, :delimiter=>',') assert_equal 123, H.from("123.0", :type=>Integer, :delimiter=>',') assert_equal 12.456, H.from("12.456", :delimiter=>',') assert_equal 1.456, H.from("1.456", :delimiter=>',') assert_equal 1.0, H.from("1", :delimiter=>',') assert_equal 1234.567, H.from("1,234.567", :delimiter=>',') assert_equal 1234.567, H.from("1234.567", :delimiter=>',') assert_equal 41234.567, H.from("41,234.567", :delimiter=>',') assert_equal 241234.567, H.from("241,234.567", :delimiter=>',') assert_equal -123.456, H.from("-123.456", :delimiter=>',') assert_equal -123.0, H.from("-123.0", :delimiter=>',') assert_equal -123.0, H.from("-123", :delimiter=>',') assert_equal -12.456, H.from("-12.456", :delimiter=>',') assert_equal -1234.567, H.from("-1,234.567", :delimiter=>',') end should "parse dms" do assert_equal 41.508473, H.dms_from("41° 30' 30.5028\" N") assert_equal 41.508473, H.dms_from("41° 30' 30.5028\"") assert_equal 41.508473, H.dms_from("41° 30' 30.5028\"N") assert_equal 41.508473, H.dms_from("N 41° 30' 30.5028\"") assert_equal 41.508473, H.dms_from("N41° 30' 30.5028\"") assert_equal -41.508473, H.dms_from("41° 30' 30.5028\" S") assert_equal -41.508473, H.dms_from("-41° 30' 30.5028\"") assert_equal -41.508473, H.dms_from("41° 30' 30.5028\"S") assert_equal -41.508473, H.dms_from("S 41° 30' 30.5028\"") assert_equal -41.508473, H.dms_from("S41° 30' 30.5028\"") assert_equal 41.508473, H.dms_from("41° 30' 30.5028\" E") assert_equal 41.508473, H.dms_from("41° 30' 30.5028\"") assert_equal 41.508473, H.dms_from("41° 30' 30.5028\"E") assert_equal 41.508473, H.dms_from("E 41° 30' 30.5028\"") assert_equal 41.508473, H.dms_from("E41° 30' 30.5028\"") assert_equal -41.508473, H.dms_from("41° 30' 30.5028\" W") assert_equal -41.508473, H.dms_from("-41° 30' 30.5028\"") assert_equal -41.508473, H.dms_from("41° 30' 30.5028\"W") assert_equal -41.508473, H.dms_from("W 41° 30' 30.5028\"") assert_equal -41.508473, H.dms_from("W41° 30' 30.5028\"") assert_equal 41.508473, H.dms_from("41° 30' 30.5028 N") assert_equal 41.508473, H.dms_from("41° 30' 30.5028") assert_equal 41.508473, H.dms_from("41° 30' 30.5028N") assert_equal 41.508473, H.dms_from("N 41° 30' 30.5028") assert_equal 41.508473, H.dms_from("N41° 30' 30.5028") assert_equal -41.508473, H.dms_from("41° 30' 30.5028 S") assert_equal -41.508473, H.dms_from("-41° 30' 30.5028") assert_equal -41.508473, H.dms_from("41° 30' 30.5028S") assert_equal -41.508473, H.dms_from("S 41° 30' 30.5028") assert_equal -41.508473, H.dms_from("S41° 30' 30.5028") assert_equal 41.508473, H.dms_from("41° 30' 30.5028 E") assert_equal 41.508473, H.dms_from("41° 30' 30.5028") assert_equal 41.508473, H.dms_from("41° 30' 30.5028E") assert_equal 41.508473, H.dms_from("E 41° 30' 30.5028") assert_equal 41.508473, H.dms_from("E41° 30' 30.5028") assert_equal -41.508473, H.dms_from("41° 30' 30.5028 W") assert_equal -41.508473, H.dms_from("-41° 30' 30.5028") assert_equal -41.508473, H.dms_from("41° 30' 30.5028W") assert_equal -41.508473, H.dms_from("W 41° 30' 30.5028") assert_equal -41.508473, H.dms_from("W41° 30' 30.5028") assert_equal 41.508473, H.dms_from("41 30' 30.5028\" N") assert_equal 41.508473, H.dms_from("41 30' 30.5028\"") assert_equal 41.508473, H.dms_from("41 30' 30.5028\"N") assert_equal 41.508473, H.dms_from("N 41 30' 30.5028\"") assert_equal 41.508473, H.dms_from("N41 30' 30.5028\"") assert_equal -41.508473, H.dms_from("41 30' 30.5028\" S") assert_equal -41.508473, H.dms_from("-41 30' 30.5028\"") assert_equal -41.508473, H.dms_from("41 30' 30.5028\"S") assert_equal -41.508473, H.dms_from("S 41 30' 30.5028\"") assert_equal -41.508473, H.dms_from("S41 30' 30.5028\"") assert_equal 41.508473, H.dms_from("41 30' 30.5028\" E") assert_equal 41.508473, H.dms_from("41 30' 30.5028\"") assert_equal 41.508473, H.dms_from("41 30' 30.5028\"E") assert_equal 41.508473, H.dms_from("E 41 30' 30.5028\"") assert_equal 41.508473, H.dms_from("E41 30' 30.5028\"") assert_equal -41.508473, H.dms_from("41 30' 30.5028\" W") assert_equal -41.508473, H.dms_from("-41 30' 30.5028\"") assert_equal -41.508473, H.dms_from("41 30' 30.5028\"W") assert_equal -41.508473, H.dms_from("W 41 30' 30.5028\"") assert_equal -41.508473, H.dms_from("W41 30' 30.5028\"") assert_equal 41.775, H.dms_from("41° 46.5'") assert_equal 41.775, H.dms_from("41° 46.5' N") assert_equal 41.775, H.dms_from("41° 46.5'N") assert_equal 41.775, H.dms_from("N41° 46.5'") assert_equal 41.775, H.dms_from("N 41° 46.5'") assert_equal -41.775, H.dms_from("-41° 46.5'") assert_equal -41.775, H.dms_from("41° 46.5' S") assert_equal -41.775, H.dms_from("41° 46.5'S") assert_equal -41.775, H.dms_from("S41° 46.5'") assert_equal -41.775, H.dms_from("S 41° 46.5'") assert_equal 41.775, H.dms_from("41 46.5'") assert_equal 41.775, H.dms_from("41 46.5' N") assert_equal 41.775, H.dms_from("41 46.5'N") assert_equal 41.775, H.dms_from("N41 46.5'") assert_equal 41.775, H.dms_from("N 41 46.5'") assert_equal -41.775, H.dms_from("-41 46.5'") assert_equal -41.775, H.dms_from("41 46.5' S") assert_equal -41.775, H.dms_from("41 46.5'S") assert_equal -41.775, H.dms_from("S41 46.5'") assert_equal -41.775, H.dms_from("S 41 46.5'") assert_equal 41.775, H.dms_from("41° 46.5") assert_equal 41.775, H.dms_from("41° 46.5 N") assert_equal 41.775, H.dms_from("41° 46.5N") assert_equal 41.775, H.dms_from("N41° 46.5") assert_equal 41.775, H.dms_from("N 41° 46.5") assert_equal -41.775, H.dms_from("-41° 46.5") assert_equal -41.775, H.dms_from("41° 46.5 S") assert_equal -41.775, H.dms_from("41° 46.5S") assert_equal -41.775, H.dms_from("S41° 46.5") assert_equal -41.775, H.dms_from("S 41° 46.5") assert_equal 41.775, H.dms_from("41.775°") assert_equal 41.775, H.dms_from("41.775° N") assert_equal 41.775, H.dms_from("41.775°N") assert_equal 41.775, H.dms_from("N41.775°") assert_equal 41.775, H.dms_from("N 41.775°") assert_equal -41.775, H.dms_from("-41.775°") assert_equal -41.775, H.dms_from("41.775° S") assert_equal -41.775, H.dms_from("41.775°S") assert_equal -41.775, H.dms_from("S41.775°") assert_equal -41.775, H.dms_from("S 41.775°") assert_equal 41.775, H.dms_from("41.775") assert_equal 41.775, H.dms_from("41.775 N") assert_equal 41.775, H.dms_from("41.775N") assert_equal 41.775, H.dms_from("N41.775") assert_equal 41.775, H.dms_from("N 41.775") assert_equal -41.775, H.dms_from("-41.775") assert_equal -41.775, H.dms_from("41.775 S") assert_equal -41.775, H.dms_from("41.775S") assert_equal -41.775, H.dms_from("S41.775") assert_equal -41.775, H.dms_from("S 41.775") assert_equal 0.508473, H.dms_from("0° 30' 30.5028\" N") assert_equal 0.508473, H.dms_from("0° 30' 30.5028\"") assert_equal 0.508473, H.dms_from("0° 30' 30.5028\"N") assert_equal 0.508473, H.dms_from("N 0° 30' 30.5028\"") assert_equal 0.508473, H.dms_from("N0° 30' 30.5028\"") assert_equal -0.508473, H.dms_from("0° 30' 30.5028\" S") assert_equal -0.508473, H.dms_from("-0° 30' 30.5028\"") assert_equal -0.508473, H.dms_from("0° 30' 30.5028\"S") assert_equal -0.508473, H.dms_from("S 0° 30' 30.5028\"") assert_equal -0.508473, H.dms_from("S0° 30' 30.5028\"") assert_equal 0.508473, H.dms_from("0° 30' 30.5028\" E") assert_equal 0.508473, H.dms_from("0° 30' 30.5028\"") assert_equal 0.508473, H.dms_from("0° 30' 30.5028\"E") assert_equal 0.508473, H.dms_from("E 0° 30' 30.5028\"") assert_equal 0.508473, H.dms_from("E0° 30' 30.5028\"") assert_equal -0.508473, H.dms_from("0° 30' 30.5028\" W") assert_equal -0.508473, H.dms_from("-0° 30' 30.5028\"") assert_equal -0.508473, H.dms_from("0° 30' 30.5028\"W") assert_equal -0.508473, H.dms_from("W 0° 30' 30.5028\"") assert_equal -0.508473, H.dms_from("W0° 30' 30.5028\"") assert_equal 0.508473, H.dms_from("0 30' 30.5028\" N") assert_equal 0.508473, H.dms_from("0 30' 30.5028\"") assert_equal 0.508473, H.dms_from("0 30' 30.5028\"N") assert_equal 0.508473, H.dms_from("N 0 30' 30.5028\"") assert_equal 0.508473, H.dms_from("N0 30' 30.5028\"") assert_equal -0.508473, H.dms_from("0 30' 30.5028\" S") assert_equal -0.508473, H.dms_from("-0 30' 30.5028\"") assert_equal -0.508473, H.dms_from("0 30' 30.5028\"S") assert_equal -0.508473, H.dms_from("S 0 30' 30.5028\"") assert_equal -0.508473, H.dms_from("S0 30' 30.5028\"") assert_equal 0.508473, H.dms_from("0 30' 30.5028\" E") assert_equal 0.508473, H.dms_from("0 30' 30.5028\"") assert_equal 0.508473, H.dms_from("0 30' 30.5028\"E") assert_equal 0.508473, H.dms_from("E 0 30' 30.5028\"") assert_equal 0.508473, H.dms_from("E0 30' 30.5028\"") assert_equal -0.508473, H.dms_from("0 30' 30.5028\" W") assert_equal -0.508473, H.dms_from("-0 30' 30.5028\"") assert_equal -0.508473, H.dms_from("0 30' 30.5028\"W") assert_equal -0.508473, H.dms_from("W 0 30' 30.5028\"") assert_equal -0.508473, H.dms_from("W0 30' 30.5028\"") assert_equal 0.508473, H.dms_from("30' 30.5028\" N") assert_equal 0.508473, H.dms_from("30' 30.5028\"") assert_equal 0.508473, H.dms_from("30' 30.5028\"N") assert_equal 0.508473, H.dms_from("N 30' 30.5028\"") assert_equal 0.508473, H.dms_from("N30' 30.5028\"") assert_equal -0.508473, H.dms_from("30' 30.5028\" S") assert_equal -0.508473, H.dms_from("-30' 30.5028\"") assert_equal -0.508473, H.dms_from("30' 30.5028\"S") assert_equal -0.508473, H.dms_from("S30' 30.5028\"") assert_equal -0.508473, H.dms_from("S 30' 30.5028\"") assert_equal 0.508473, H.dms_from("30' 30.5028\" E") assert_equal 0.508473, H.dms_from("30' 30.5028\"") assert_equal 0.508473, H.dms_from("30' 30.5028\"E") assert_equal 0.508473, H.dms_from("E 30' 30.5028\"") assert_equal 0.508473, H.dms_from("E30' 30.5028\"") assert_equal -0.508473, H.dms_from("30' 30.5028\"W") assert_equal -0.508473, H.dms_from("W 30' 30.5028\"") assert_equal -0.508473, H.dms_from("W30' 30.5028\"") assert_equal 0.5, H.dms_from("30' N") assert_equal 0.5, H.dms_from("30'") assert_equal 0.5, H.dms_from("30'N") assert_equal 0.5, H.dms_from("N 30'") assert_equal 0.5, H.dms_from("N30'") assert_equal -0.5, H.dms_from("30' S") assert_equal -0.5, H.dms_from("-30'") assert_equal -0.5, H.dms_from("30'S") assert_equal -0.5, H.dms_from("S30'") assert_equal -0.5, H.dms_from("S 30'") assert_equal 0.5, H.dms_from("30' E") assert_equal 0.5, H.dms_from("30'E") assert_equal 0.5, H.dms_from("E 30'") assert_equal 0.5, H.dms_from("E30'") assert_equal -0.5, H.dms_from("30'W") assert_equal -0.5, H.dms_from("W 30'") assert_equal -0.5, H.dms_from("W30'") assert_equal 0.01235, H.dms_from("44.46\"N") assert_equal 0.01235, H.dms_from("44.46\"") assert_equal -0.01235, H.dms_from("-44.46\"") assert_equal 0.0, H.dms_from("0° 0' 0\"N") assert_equal 0.0, H.dms_from("0° 0' 0.00\"N") assert_equal 0.0, H.dms_from("0° 0' 0\"") assert_equal 0.0, H.dms_from("0° 0' 0.00\"") assert_equal 0.0, H.dms_from("-0° 0' 0\"") assert_equal 0.0, H.dms_from("0° 0' 0\"S") assert_equal 0.0, H.dms_from("0° 0' 0.00\"S") assert_equal 0.0, H.dms_from("S0° 0' 0\"") assert_equal 0.0, H.dms_from("S0° 0' 0.00\"") assert_equal 0.0, H.dms_from("S 0° 0' 0\"") assert_equal 0.0, H.dms_from("S 0° 0' 0.00\"") assert_equal 0.0, H.dms_from("0 0' 0\"") assert_equal 0.0, H.dms_from("0 0' 0.00\"") assert_equal 0.0, H.dms_from("0 0' 0\"N") assert_equal 0.0, H.dms_from("0° 0'N") assert_equal 0.0, H.dms_from("0° 0.00'N") assert_equal 0.0, H.dms_from("0° 0'") assert_equal 0.0, H.dms_from("0° 0.00'") assert_equal 0.0, H.dms_from("0° 0'S") assert_equal 0.0, H.dms_from("0° 0.00' S") assert_equal 0.0, H.dms_from("0 0'") assert_equal 0.0, H.dms_from("-0° 0'") assert_equal 0.0, H.dms_from("-0 0'") assert_equal 0.0, H.dms_from("0°") assert_equal 0.0, H.dms_from("0°N") assert_equal 0.0, H.dms_from("0.00°") assert_equal 0.0, H.dms_from("0") assert_equal 0.0, H.dms_from("0.0") assert_equal 0.0, H.dms_from("0.0N") assert_equal 0.0, H.dms_from("0.0S") assert_equal 0.0, H.dms_from("0'") assert_equal 0.0, H.dms_from("-0.00'") assert_equal 0.0, H.dms_from("-0.00\"") assert_equal 0.0, H.dms_from("0.00\"") assert_raise(ArgumentError){H.dms_from("zdfseer")} assert_raise(ArgumentError){H.dms_from("++0.3")} assert_raise(ArgumentError){H.dms_from("+-0.3")} assert_equal 41.508473, H.dms_from("41° 30min 30.5028\" N", :min_seps=>'min') assert_equal 41.508473, H.dms_from("41° 30' 30.5028sec N", :sec_seps=>'sec') assert_equal -41.508473, H.dms_from("41d 30m 30.5028s S", :sec_seps=>'s', :min_seps=>'m', :deg_seps=>'d') assert_equal -41.508473, H.dms_from("41d 30m 30.5028sS", :sec_seps=>'s', :min_seps=>'m', :deg_seps=>'d') assert_equal -41.508473, H.dms_from("S41d 30m 30.5028s", :sec_seps=>'s', :min_seps=>'m', :deg_seps=>'d') assert_equal 41.508473, H.dms_from("41d 30m 30.5028s N", :sec_seps=>'s', :min_seps=>'m', :deg_seps=>'d') assert_equal 41.508473, H.dms_from("41d 30m 30.5028sN", :sec_seps=>'s', :min_seps=>'m', :deg_seps=>'d') assert_equal -41.508473, H.dms_from("-41d 30m 30.5028s", :sec_seps=>'s', :min_seps=>'m', :deg_seps=>'d') assert_equal -41.508473, H.dms_from("41d 30m 30.5028sX", :sec_seps=>'s', :min_seps=>'m', :deg_seps=>'d', :south=>'X') assert_equal -41.508473, H.dms_from("41d 30m 30.5028s X", :sec_seps=>'s', :min_seps=>'m', :deg_seps=>'d', :south=>'X') assert_equal -41.508473, H.dms_from("41 30m 30.5028s X", :sec_seps=>'s', :min_seps=>'m', :deg_seps=>'d', :south=>'X') end should "format as dms" do assert_equal "41° 30' 30.5028\"", H.dms_to(41.508473, :prefix=>false) assert_equal "41° 30' 30.5028\"", H.dms_to(41.508473, :prefix=>true) assert_equal "-41° 30' 30.5028\"", H.dms_to(-41.508473, :prefix=>false) assert_equal "-41° 30' 30.5028\"", H.dms_to(-41.508473, :prefix=>true) assert_equal "41° 30' 30.5028\" N", H.dms_to(41.508473, :prefix=>false, :latitude=>true) assert_equal "N 41° 30' 30.5028\"", H.dms_to(41.508473, :prefix=>true, :latitude=>true) assert_equal "41° 30' 30.5028\" S", H.dms_to(-41.508473, :prefix=>false, :latitude=>true) assert_equal "S 41° 30' 30.5028\"", H.dms_to(-41.508473, :prefix=>true, :latitude=>true) assert_equal "41° 30' 30.5028\" E", H.dms_to(41.508473, :prefix=>false, :longitude=>true) assert_equal "E 41° 30' 30.5028\"", H.dms_to(41.508473, :prefix=>true, :longitude=>true) assert_equal "41° 30' 30.5028\" W", H.dms_to(-41.508473, :prefix=>false, :longitude=>true) assert_equal "W 41° 30' 30.5028\"", H.dms_to(-41.508473, :prefix=>true, :longitude=>true) assert_equal "41° 30' N", H.dms_to(41.5, :prefix=>false, :latitude=>true) assert_equal "41° N", H.dms_to(41.0, :prefix=>false, :latitude=>true) assert_equal "41° N", H.dms_to(41, :prefix=>false, :latitude=>true) assert_equal "41° 30' 30.50\"", H.dms_to(41.508473, :prefix=>false, :precision=>2) assert_equal "41° 30' 30.503\"", H.dms_to(41.508473, :prefix=>false, :precision=>3) end end