From: Josh Cheek Date: 2010-04-14T11:36:34+09:00 Subject: Re: Method to groom a string to floating point representation --000e0cd13a0208fd8b0484293fe3 Content-Type: text/plain; charset=windows-1252 Content-Transfer-Encoding: quoted-printable On Tue, Apr 13, 2010 at 6:20 AM, Alex DeCaria wrote: > Delete any decimals in an exponent: > '245e7.6' =3D> '2.45e76' > Where did the dot in between 2 and 4 come from? Am I interpreting the Strin= g or just cleaning it? > Delete any extra or misplaced + or =96 signs: > '+45-68+e+45-' =3D> '4568e+45' > > Delete any extra or misplaced =91e=92 or =91E=92 characters (first occura= nce of > '+e4.67e-7' =3D> '+4.67e-7' > > Why does the plus in front of 45 in the first one go away, but the plus in front of the e in the second one stays? ----- This is what I have so far, please check and correct any tests that should be different def clean_string( str , options =3D Hash.new ) str =3D~ /\A([-+]?)([^eE.]*\.?)([^eE]*)((?:[eE][+-]?)?)([^Z]*)\Z/ posneg , prepre , postpre , e , post =3D $1 , $2 , $3 , $4 , $5 posneg + prepre + postpre.gsub(/[^0-9]/,'') + e + post.gsub(/[^0-9]/,'') end require 'test/unit' class TestCleanString < Test::Unit::TestCase def test_delete_chars assert_equal '-24.5e45' , clean_string('-24.5fge4x5') end def test_delete_extra_decimal assert_equal '2.45' , clean_string('2.4.5') assert_equal '2.45' , clean_string('2..45') assert_equal '2.45' , clean_string('2...45') end def test_delete_extra_decimal_in_exponent assert_equal '245e76' , clean_string('245e7.6') # you said this should be '2.45e76' , but where did first dot come from? end def test_delete_extra_or_misplaced_pos_and_neg_signs assert_equal '4568e+45' , clean_string('+45-68+e+45-') end def test_delete_extra_or_misplaced_e_or_E assert_equal '4.67e67' , clean_string('4.67e6e-7') assert_equal '+4.67e-7' , clean_string('+e4.67e-7') end end --000e0cd13a0208fd8b0484293fe3--