Is there an equivalent in VBScript to JScript's "toExponential" method? I
need to display a number in exponential format and haven't found a means to
do that yet.

Thanks,
Karen

Re: Scientific Notation by ahmed_sami

ahmed_sami
Tue May 24 12:38:25 CDT 2005

please give an example
cheers,
S@mi
"karena" <karena@discussions.microsoft.com> wrote in message
news:F4A1B344-727C-4C64-9A97-D04253E6C0C9@microsoft.com...
> Is there an equivalent in VBScript to JScript's "toExponential" method? I
> need to display a number in exponential format and haven't found a means
> to
> do that yet.
>
> Thanks,
> Karen



Re: Scientific Notation by karena

karena
Tue May 24 13:11:08 CDT 2005

72.5 needs to be written to a text file as 7.25E+01
and 0.0038472 has to be written as 3.8472E-03

"S??I" wrote:

> please give an example
> cheers,
> S@mi
> "karena" <karena@discussions.microsoft.com> wrote in message
> news:F4A1B344-727C-4C64-9A97-D04253E6C0C9@microsoft.com...
> > Is there an equivalent in VBScript to JScript's "toExponential" method? I
> > need to display a number in exponential format and haven't found a means
> > to
> > do that yet.
> >
> > Thanks,
> > Karen
>
>
>

Re: Scientific Notation by Bob

Bob
Tue May 24 13:27:51 CDT 2005

No, you'll have to write your own. I've never written one so I can't help
you without writing it myself, which I don't have time to do right now. Do
you need your solution to be aware of significant digits?

Bob Barrows
karena wrote:
> 72.5 needs to be written to a text file as 7.25E+01
> and 0.0038472 has to be written as 3.8472E-03
>
> "S??I" wrote:
>
>> please give an example
>> cheers,
>> S@mi
>> "karena" <karena@discussions.microsoft.com> wrote in message
>> news:F4A1B344-727C-4C64-9A97-D04253E6C0C9@microsoft.com...
>>> Is there an equivalent in VBScript to JScript's "toExponential"
>>> method? I need to display a number in exponential format and
>>> haven't found a means to
>>> do that yet.
>>>
>>> Thanks,
>>> Karen

--
Microsoft MVP -- ASP/ASP.NET
Please reply to the newsgroup. The email account listed in my From
header is my spam trap, so I don't check it very often. You will get a
quicker response by posting to the newsgroup.



Re: Scientific Notation by Bob

Bob
Tue May 24 15:13:32 CDT 2005

karena wrote:
> 72.5 needs to be written to a text file as 7.25E+01
> and 0.0038472 has to be written as 3.8472E-03
>
OK, I was intrigued. Here is a quickie (it needs error-handling and input
validation) that will handle positive numbers between 1.0E-308 and 1.0E+308:

function NumToExpNot(byval val)
dim i, newval, mult, sign
val = cdbl(val)
if val > 0 then
if val >= 1 then
mult=-1
sign="+"
else
mult=1
sign="-"
end if
i=0
newval=val
do until newval >= 1 AND newval < 10
i = i + 1
newval = val * 10^(mult * i)
loop
newval=newval & "E" & sign & right("000" & i,3)
NumToExpNot=newval
else
end if
end function

This should give you the idea if you need to handle negative numbers ...

HTH,
Bob Barrows
--
Microsoft MVP -- ASP/ASP.NET
Please reply to the newsgroup. The email account listed in my From
header is my spam trap, so I don't check it very often. You will get a
quicker response by posting to the newsgroup.



Re: Scientific Notation by Joe

Joe
Wed May 25 09:15:18 CDT 2005

Hi Bob

"Bob Barrows [MVP]" <reb01501@NOyahoo.SPAMcom> wrote in message
news:u3FEQ0JYFHA.612@TK2MSFTNGP12.phx.gbl...
> karena wrote:
>> 72.5 needs to be written to a text file as 7.25E+01
>> and 0.0038472 has to be written as 3.8472E-03
>>
> OK, I was intrigued. Here is a quickie (it needs error-handling and input
> validation) that will handle positive numbers between 1.0E-308 and
> 1.0E+308:
>
> function NumToExpNot(byval val)
> dim i, newval, mult, sign
> val = cdbl(val)
> if val > 0 then
> if val >= 1 then
> mult=-1
> sign="+"
> else
> mult=1
> sign="-"
> end if
> i=0
> newval=val
> do until newval >= 1 AND newval < 10
> i = i + 1
> newval = val * 10^(mult * i)
> loop
> newval=newval & "E" & sign & right("000" & i,3)
> NumToExpNot=newval
> else
> end if
> end function
>
> This should give you the idea if you need to handle negative numbers ...
>
> HTH,
> Bob Barrows
> --
> Microsoft MVP -- ASP/ASP.NET
> Please reply to the newsgroup. The email account listed in my From
> header is my spam trap, so I don't check it very often. You will get a
> quicker response by posting to the newsgroup.
>

The problems are: (1) longer double-precision numerics automatically
convert to the E notation when you try to manipulate them as strings (a
curiousity of BASIC); and (2) double-precision rounding sets in even at low
powers. Here's a slightly updated version of an old QuickBASIC function
that works with any numeric value within double-precision range, or with any
length and without double-precision rounding, if submitted as a string, by
manipulating the numeric argument purely as a string. Check the differences
in examples 3 and 4.

-----
n1= 72.5
n2= .0038472
n3= 500000000000000000005.000000000005
n4= "500000000000000000005.000000000005"

wscript.echo sciEFmt(n1) _
& vbCr & sciEFmt(n2) _
& vbCR & sciEFmt(n3) _
& vbCR & sciEFmt(n4)

function SciEFmt (byVal vxNum)
' returns string, "n.nn...E+/-n"
' at least 1 decimal position
' at least 2 power positions
' vxNum= positive numeric value or string
' returns 0, "" or Empty as "0"
' returns Empty if not numeric or negative

on error resume next
dim vsNum: vsNum= ucase(cstr(vxNum))
if err then err.clear: exit function
on error goto 0

if (vsNum="") then sciEFmt= 0: exit function

if not isnumeric(vxNum) then exit function
if (vxNum<0) then exit function

dim viDelim, viE, vsSgn: do
viDelim= instr(vsNum, "E"): if viDelim then
vsSgn= mid(vsNum, viDelim +1, 1)
viE= mid(vsNum, viDelim +2)
vsNum= left(vsNum, viDelim -1)
exit do
end if

do while (right(vsNum, 1)="0")
vsNum= left(vsNum, len(vsNum) -1)
loop

if (vsNum="") then sciEFmt= "0": exit function

viDelim= instr(vsNum & ".", ".")

dim vsInt: vsInt= left(vsNum, viDelim -1)
do while (left(vsInt, 1)="0")
vsInt= mid(vsInt, 2)
loop

dim vsFrac: vsFrac= mid(vsNum, viDelim +1)
do while (right(vsFrac, 1)="0")
vsInt= left(vsFrac, len(vsFrac) -1)
loop

if (len(vsInt & vsFrac)=0) then sciEFmt= "0": _
exit function

select case vsInt
case "": viE= 1
do while (left(vsFrac, 1)="0")
vsFrac= mid(vsFrac, 2): viE= viE +1
loop: vsNum= vsFrac: vsSgn= "-"

case else: viE= len(vsInt) -1
vsNum= vsInt & vsFrac: vsSgn= "+"
end select
loop until true

sciEFmt= left(vsNum, 1) & "." _
& array("0", mid(vsNum, 2))(sgn(len(vsNum) -1)) _
& "E" & vsSgn & array("", "0")(abs(viE<10)) & viE
end function
-----

Joe Earnest



Re: Scientific Notation by Bob

Bob
Wed May 25 13:00:37 CDT 2005

Joe Earnest wrote:
> Hi Bob
>
> The problems are: (1) longer double-precision numerics automatically
> convert to the E notation when you try to manipulate them as strings
> (a curiousity of BASIC); and

Yes, I did forget about that - I said it was a quickie ... :-)

> (2) double-precision rounding sets in
> even at low powers.

Hmmm - yes, I never noticed that. Thanks for the catch.

Bob
--
Microsoft MVP -- ASP/ASP.NET
Please reply to the newsgroup. The email account listed in my From
header is my spam trap, so I don't check it very often. You will get a
quicker response by posting to the newsgroup.



Re: Scientific Notation by Joe

Joe
Wed May 25 17:13:56 CDT 2005

Oops,

My "slight updating" produced some bad code that didn't show up in the
examples. This should be better.

-----
n1= 72.5
n2= .0038472
n3= 5000000000000005
n4= "5000000000000005"

wscript.echo sciEFmt(n1) _
& vbCr & sciEFmt(n2) _
& vbCR & sciEFmt(n3) _
& vbCR & sciEFmt(n4)

function SciEFmt (byVal vxNum)
' returns string, "n.nn...E+/-n"
' at least 1 decimal position
' at least 2 power positions
' vxNum= positive numeric value or string
' returns 0, "" or Empty as "0"
' returns Empty if not numeric or negative

on error resume next
dim vsNum: vsNum= ucase(cstr(vxNum))
if err then err.clear: exit function
on error goto 0

if (vsNum="") then sciEFmt= 0: exit function

if not isnumeric(vxNum) then exit function
if (vxNum<0) then exit function

dim viDelim, viE, vsSgn: do
viDelim= instr(vsNum, "E"): if viDelim then
vsSgn= mid(vsNum, viDelim +1, 1)
viE= mid(vsNum, viDelim +2)
vsNum= left(vsNum, viDelim -1)
if (instr(vsNum, ".")=0) then vsNum= vsNum & ".0"
exit do
end if

do while (left(vsNum, 1)="0")
vsNum= mid(vsNum, 2)
loop

viDelim= instr(vsNum & ".", ".")

dim vsInt: vsInt= left(vsNum, viDelim -1)
dim vsFrac: vsFrac= mid(vsNum, viDelim +1)
if (len(vsInt & vsFrac)=0) then sciEFmt= "0": _
exit function

do while (right(vsFrac, 1)="0")
vsFrac= left(vsFrac, len(vsFrac) -1)
loop

select case vsInt
case "": viE= 1
do while (left(vsFrac, 1)="0")
vsFrac= mid(vsFrac, 2): viE= viE +1
loop: vsNum= vsFrac: vsSgn= "-"

case else: viE= len(vsInt) -1
vsNum= vsInt & vsFrac: vsSgn= "+"
end select

vsNum= left(vsNum, 1) & "." _
& array("0", mid(vsNum, 2))(sgn(len(vsNum) -1))
loop until true

sciEFmt= vsNum & "E" _
& vsSgn & array("", "0")(abs(viE<10)) & viE
end function
-----

Joe Earnest