zero placeholder exponential sprintf?

B

Bil Kleb

Hi,

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

% ruby -e "puts '%.6e' % [ 1.123450e+2 ]"
1.123450e+02

Possible?

Thanks,
 
7

7stud --

Bil said:
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.
 
H

Harry Kakueki

Hi,

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

% ruby -e "puts '%.6e' % [ 1.123450e+2 ]"
1.123450e+02

Possible?

Thanks,

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
 
B

Bil Kleb

Harry said:
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,
 
H

Harry Kakueki

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
 
B

Brian Candler

Bil said:
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
 
H

Harry Kakueki

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
 
H

Harry Kakueki

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top