From: Harry Kakueki Date: 2009-08-06T06:51:06+09:00 Subject: Re: zero placeholder exponential sprintf? On Wed, Aug 5, 2009 at 10:57 PM, Harry Kakueki wrote: >> > > x = 1.12345e2 > > sig,y = 9, Math.log10(x).floor + 1 > p (x*10**(y*-1)).to_s[0..sig+1].ljust(sig+2,"0") + "e+" + > y.to_s.rjust(2,"0") if y >= 0 > p (x*10**(y*-1)).to_s[0..sig+1].ljust(sig+2,"0") + "e-" + > y.abs.to_s.rjust(2,"0") if y < 0 > > > Harry > This is the same code I posted earlier, just a little DRYer. x = 1.12345e2 s,y,h = 7, Math.log10(x).floor+1,{-1=>"-",0=>"+",1=>"+"} p (x*10**(y*-1)).to_s[0..s+1].ljust(s+2,"0")+"e"+h[(y<=>0)]+y.abs.to_s.rjust(2,"0") Harry -- A Look into Japanese Ruby List in English http://www.kakueki.com/ruby/list.html