From: Bil Kleb Date: 2009-08-05T20:30:06+09:00 Subject: Re: zero placeholder exponential sprintf? Harry Kakueki wrote: > On Wed, Aug 5, 2009 at 7:00 AM, Bil Kleb wrote: >> We have a request for 0.112345e+03 instead of, >> >> % ruby -e "puts '%.6e' % [ 1.123450e+2 ]" >> 1.123450e+02 > I may be missing what you are after, but.... > Does it need to be sprintf? Sorry for the confusion, String::% uses Kernel::sprintf -- see http://www.ruby-doc.org/core/classes/String.html#M000770 So, to match my subject line better and correct the desired formatted number: ruby -e "puts sprintf('%.6e',1.123450e+2)" #=> 1.123450e+02 but I want '0.112345e+03', i.e., a leading zero placeholder. @drbrain says this probably violates IEEE, http://twitter.com/drbrain/statuses/3132296591 http://twitter.com/drbrain/statuses/3132322954 > require 'bigdecimal' > p BigDecimal.new(1.12345e+2.to_s).to_s #=> "0.112345E3" Hmmm, now just need the '+0' part in the exponent and control of the number of decimal places? Regards, -- Bil Kleb http://fun3d.larc.nasa.gov