# Zero placeholder exponential sprintf?

**URL:** <https://www.ruby-forum.com/t/zero-placeholder-exponential-sprintf/173213>\
**Category:** Ruby\
**Created:** [August 5, 2009, 12:03am UTC](https://www.ruby-forum.com/t/zero-placeholder-exponential-sprintf/173213 "2009-08-05T00:03:23Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Bil\_K](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/848f3c/32.png) [@Bil\_K](https://www.ruby-forum.com/u/Bil_K)\
**Post date:** [August 5, 2009, 12:03am UTC](https://www.ruby-forum.com/t/zero-placeholder-exponential-sprintf/173213/1 "2009-08-05T00:03:23Z")

</div>

Hi,

We have a request for 0.112345e+02 instead of,

% ruby -e “puts ‘%.6e’ % [1.123450e+2]”  
1.123450e+02

Possible?

Thanks,

---

<div class="post-metadata">

**Author:** ![Bil\_K](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/848f3c/32.png) [@Bil\_K](https://www.ruby-forum.com/u/Bil_K)\
**Post date:** [August 5, 2009, 2:43am UTC](https://www.ruby-forum.com/t/zero-placeholder-exponential-sprintf/173213/2 "2009-08-05T02:43:56Z")

</div>

Bil K. wrote:

> Hi,
> 
> We have a request for 0.112345e+02 instead of,
> 
> % ruby -e “puts ‘%.6e’ % [1.123450e+2]”  
> 1.123450e+02
> 
> Possible?
> 
> Thanks,

data = 112.3450  
result = “%e” % [data]

puts result #1.123450e+02

pieces = result.split(".")  
result = “.%s” % [pieces.join()]

puts result  
–output:–  
.1123450e+02

Of course, it wouldn’t make any sense to do that.

---

<div class="post-metadata">

**Author:** ![Bil\_K](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/848f3c/32.png) [@Bil\_K](https://www.ruby-forum.com/u/Bil_K)\
**Post date:** [August 5, 2009, 3:07am UTC](https://www.ruby-forum.com/t/zero-placeholder-exponential-sprintf/173213/3 "2009-08-05T03:07:10Z")

</div>

On Wed, Aug 5, 2009 at 7:00 AM, Bil K.[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> –  
> Bil K.  
> [http://fun3d.larc.nasa.gov](http://fun3d.larc.nasa.gov)

I may be missing what you are after, but…  
Does it need to be sprintf?

require ‘bigdecimal’  
p BigDecimal.new(1.12345e+2.to\_s).to\_s #=\> “0.112345E3”

Harry

---

<div class="post-metadata">

**Author:** ![Bil\_K](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/848f3c/32.png) [@Bil\_K](https://www.ruby-forum.com/u/Bil_K)\
**Post date:** [August 5, 2009, 3:58pm UTC](https://www.ruby-forum.com/t/zero-placeholder-exponential-sprintf/173213/4 "2009-08-05T15:58:39Z")

</div>

> 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

---

<div class="post-metadata">

**Author:** ![Bil\_K](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/848f3c/32.png) [@Bil\_K](https://www.ruby-forum.com/u/Bil_K)\
**Post date:** [August 5, 2009, 1:30pm UTC](https://www.ruby-forum.com/t/zero-placeholder-exponential-sprintf/173213/5 "2009-08-05T13:30:32Z")

</div>

Harry K. wrote:

> On Wed, Aug 5, 2009 at 7:00 AM, Bil K.[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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

[class String - RDoc Documentation](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/3132296591)  
[http://twitter.com/drbrain/statuses/3132322954](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,

---

<div class="post-metadata">

**Author:** ![Bil\_K](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/848f3c/32.png) [@Bil\_K](https://www.ruby-forum.com/u/Bil_K)\
**Post date:** [August 5, 2009, 11:51pm UTC](https://www.ruby-forum.com/t/zero-placeholder-exponential-sprintf/173213/6 "2009-08-05T23:51:37Z")

</div>

On Wed, Aug 5, 2009 at 10:57 PM, Harry K.[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> 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

---

<div class="post-metadata">

**Author:** ![Bil\_K](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/848f3c/32.png) [@Bil\_K](https://www.ruby-forum.com/u/Bil_K)\
**Post date:** [August 5, 2009, 4:02pm UTC](https://www.ruby-forum.com/t/zero-placeholder-exponential-sprintf/173213/7 "2009-08-05T16:02:36Z")

</div>

Bil K. wrote:

> Hi,
> 
> We have a request for 0.112345e+02 instead of,
> 
> % ruby -e “puts ‘%.6e’ % [1.123450e+2]”  
> 1.123450e+02
> 
> Possible?
> 
> Thanks,

I’m not sure this works for all possible cases, but you could just  
manipulate the string a bit:

class Float  
def fmt(prec=6)  
str = “%.\*e” % [prec-1, self]  
if str =~ /^([1-9]).(\d+)e([±])(\d+)$/  
str = “0.%s%se%s%02d” % [$1,$2,$3,$4.to\_i]  
end  
str  
end  
end

a = 1.123450e+2  
puts a.fmt

---

<div class="post-metadata">

**Author:** ![Bil\_K](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/848f3c/32.png) [@Bil\_K](https://www.ruby-forum.com/u/Bil_K)\
**Post date:** [August 6, 2009, 1:12am UTC](https://www.ruby-forum.com/t/zero-placeholder-exponential-sprintf/173213/8 "2009-08-06T01:12:23Z")

</div>

> We have a request for 0.112345e+02 instead of,
> 
> % ruby -e “puts ‘%.6e’ % [1.123450e+2]”  
> 1.123450e+02
> 
> Possible?

Sorry for posting yet again.  
Just a little adjustment.  
I’ll stop now.

x = 1.12345e2

s,y,h = 7, Math.log10(x).floor+1,{-1=\>“e-”,0=\>“e+”,1=\>“e+”}  
p  
(x_10\*\*(y_-1)).to\_s[0…s+1].ljust(s+2,“0”)+h[(y\<=\>0)]+y.abs.to\_s.rjust(2,“0”)

Harry

---

<div class="post-metadata">

**Author:** ![Bil\_K](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/848f3c/32.png) [@Bil\_K](https://www.ruby-forum.com/u/Bil_K)\
**Post date:** [August 6, 2009, 8:16am UTC](https://www.ruby-forum.com/t/zero-placeholder-exponential-sprintf/173213/9 "2009-08-06T08:16:09Z")

</div>

Bil K. wrote:

> Sorry for the confusion, String::% uses Kernel::sprintf – see
> 
> [class String - RDoc Documentation](http://www.ruby-doc.org/core/classes/String.html#M000770)

Uh, no.

class Object  
def sprintf(\*args)  
puts “inside sprintf…”  
“hello”  
end  
end

result = sprintf(“%.6e”, 1.123450e4)  
puts result

puts “%.6e” % 1.123450e4

–output:–  
inside sprintf…  
hello  
1.123450e+04
