On 5/4/07, Bil Kleb <Bil.Kleb / nasa.gov> wrote: > Rick DeNatale wrote: > > > > How's this for a start? > > Excellent! Thanks. > > All but my last test passed: > > require 'test/unit' > require 'number_format' > class TestNumberFormat < Test::Unit::TestCase > def test_some_floats > assert_equal( '%3.1f', '8.3'.to_number_format ) > assert_equal( '%05.3f', '0.500'.to_number_format ) > assert_equal( '%8.7f', '.0001170'.to_number_format ) Not sure how this one worked, it fails for me. As a matter of fact: irb(main):001:0> '%8.7f' % 0.0001170 => "0.0001170" And I haven't been able to find an sprintf format string which supresses a leading zero on a float. > assert_equal( '%7.1f', '14000.0'.to_number_format ) > assert_equal( '%9.3E', '4.480E+09'.to_number_format ) > assert_equal( '%6.1e', '3.2e-5'.to_number_format ) > assert_equal( '%6.1f', '-254.2'.to_number_format ) > end > end > > 1) Failure: > test_some_floats(TestNumberFormat) [-:11]: > <"%6.1f"> expected but was > <"%5.1f">. > > Note: made the simple float leading digit match 0 > or more to get the third test to pass. > > Puzzling the minus sign part now... I see that you figured this out. Another thing to test is that the values actually round trip. Here's my test: rick@frodo:/public/rubyscripts$ cat test_number_format.rb require 'test/unit' require 'number_format' class TestNumberFormat < Test::Unit::TestCase def test_some_floats assert_equal( '%3.1f', '8.3'.to_number_format ) assert_nf('8.3') assert_equal( '%05.3f', '0.500'.to_number_format ) assert_nf('0.500') assert_equal( '%8.7f', '.0001170'.to_number_format ) assert_nf('.0001170') assert_equal( '%7.1f', '14000.0'.to_number_format ) assert_nf('14000.0') assert_equal( '%9.3E', '4.480E+09'.to_number_format ) assert_nf('4.480E+09') assert_equal( '%6.1e', '3.2e-5'.to_number_format ) assert_nf('3.2e-5') assert_equal( '%6.1f', '-254.2'.to_number_format ) assert_nf('-254.2') end private def assert_nf(str) assert_equal(str, str.to_number_format % eval(str)) end end -- Rick DeNatale My blog on Ruby http://talklikeaduck.denhaven2.com/