Re: zero placeholder exponential sprintf?
- From: Harry Kakueki <list.push@xxxxxxxxx>
- Date: Wed, 5 Aug 2009 08:57:14 -0500
ruby -e "puts sprintf('%.6e',1.123450e+2)" #=> 1.123450e+02
but I want '0.112345e+03', i.e., a leading zero placeholder.
Hmmm, now just need the '+0' part in the exponent and control
of the number of decimal places?
I am still not sure if you require sprintf.
I am not familiar with that but I will learn about it starting tomorrow. Thanks.
This code looks a bit strange to me and it is not very DRY.
Maybe someone will have a better solution soon. There must be a better way.
It may have some problems, so check it carefully. It is the best I can
offer you this late at night.
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
--
A Look into Japanese Ruby List in English
http://www.kakueki.com/ruby/list.html
.
- Follow-Ups:
- Re: zero placeholder exponential sprintf?
- From: Harry Kakueki
- Re: zero placeholder exponential sprintf?
- References:
- zero placeholder exponential sprintf?
- From: Bil Kleb
- Re: zero placeholder exponential sprintf?
- From: Harry Kakueki
- Re: zero placeholder exponential sprintf?
- From: Bil Kleb
- zero placeholder exponential sprintf?
- Prev by Date: Re: Kind of ParsTree for 1.9.1
- Next by Date: Re: Bizarre Range behavior
- Previous by thread: Re: zero placeholder exponential sprintf?
- Next by thread: Re: zero placeholder exponential sprintf?
- Index(es):
Relevant Pages
|
Loading