Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

zero placeholder exponential sprintf?

19 views
Skip to first unread message

Bil Kleb

unread,
Aug 4, 2009, 5:19:42 PM8/4/09
to
Hi,

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

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

Possible?

Thanks,
--
Bil Kleb
http://fun3d.larc.nasa.gov

7stud --

unread,
Aug 4, 2009, 8:44:03 PM8/4/09
to
Bil Kleb 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.

--
Posted via http://www.ruby-forum.com/.

Harry Kakueki

unread,
Aug 4, 2009, 9:06:12 PM8/4/09
to

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

--
A Look into Japanese Ruby List in English
http://www.kakueki.com/ruby/list.html

Bil Kleb

unread,
Aug 5, 2009, 7:26:54 AM8/5/09
to
Harry Kakueki wrote:
> On Wed, Aug 5, 2009 at 7:00 AM, Bil Kleb<Bil....@nasa.gov> 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,

Harry Kakueki

unread,
Aug 5, 2009, 9:57:14 AM8/5/09
to
>
> 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

Brian Candler

unread,
Aug 5, 2009, 10:02:38 AM8/5/09
to
Bil Kleb 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

Harry Kakueki

unread,
Aug 5, 2009, 5:51:06 PM8/5/09
to
On Wed, Aug 5, 2009 at 10:57 PM, Harry Kakueki<list...@gmail.com> wrote:
>>
>
> 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 Kakueki

unread,
Aug 5, 2009, 7:11:47 PM8/5/09
to
>
> 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")

7stud --

unread,
Aug 6, 2009, 2:16:16 AM8/6/09
to
Bil Kleb 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

0 new messages