Zero placeholder exponential sprintf?

Hi,

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

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

Possible?

Thanks,

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.

On Wed, Aug 5, 2009 at 7:00 AM, Bil K.[email protected] wrote:


Bil K.
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

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 (x10**(y-1)).to_s[0…sig+1].ljust(sig+2,“0”) + “e+” +
y.to_s.rjust(2,“0”) if y >= 0
p (x10**(y-1)).to_s[0…sig+1].ljust(sig+2,“0”) + “e-” +
y.abs.to_s.rjust(2,“0”) if y < 0

Harry

Harry K. wrote:

On Wed, Aug 5, 2009 at 7:00 AM, Bil K.[email protected] 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,

On Wed, Aug 5, 2009 at 10:57 PM, Harry K.[email protected]
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
(x10**(y-1)).to_s[0…s+1].ljust(s+2,“0”)+“e”+h[(y<=>0)]+y.abs.to_s.rjust(2,“0”)

Harry

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

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
(x10**(y-1)).to_s[0…s+1].ljust(s+2,“0”)+h[(y<=>0)]+y.abs.to_s.rjust(2,“0”)

Harry

Bil K. wrote:

Sorry for the confusion, String::% uses Kernel::sprintf – see

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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs