# Convert scientific notation to regular numbers in VB Script

When calculating a value in VB script it returns scientific notation. So I try to convert using simple math, problem is sometimes the result in the math is also a scientific notation.

Is there a simple way to just say if the result is 1.6666666666666667E-03 then return 0.0016666?
###### Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Commented:
0
Commented:
and what is the rage of numbers you want to convert?
The mass of a proton is 1.67E-27 kg which would be a little awkward to write as
0.00000000000000000000000000167 kg
0
Author Commented:
factor = 6.0000
unit1 is null
unit2 = .01

Function calculatevalue (factor,unit1,unit2)
dim calculatedValue
dim postionOfE
dim removeSciNotation
dim expValue
dim divValue

if trim(unit1) = "" then
unit1 = 0
end if
if trim(unit2) = "" then
unit2 = 0
end if
calculatedValue = Cstr(unit1 + CDbl(unit2) / CDbl(factor))
postionOfE = InStr(calculatedValue,"E-")

if postionOfE > 0 then
expValue = Mid(calculatedValue,(postionOfE + 2))
removeSciNotation =  Left(calculatedValue,postionOfE - 2)
divValue = 1
for loopi = 1 to expValue
divValue = divValue * 10
next
CalculateEndingInvCount      = removeSciNotation / divValue
else
CalculateEndingInvCount      = unit1 + (unit2 / factor)
end if
End Function

In this example "CalculateEndingInvCount      = removeSciNotation / divValue
also is displayed in scientific notation. I want the result in just regular numbers
0
EngineerCommented:
try changing

calculatevalue      = removeSciNotation / divValue

to

CalculateEndingInvCount = Format(removeSciNotation / divValue, "0." & String(postionOfE + Len(removescientificnotation), "#"))

BTW your function does not return anything. You might like to change

calculatevalue

to

calculatevalue
0
Author Commented:
That throws a syntax error. If you can give a pseudo code then may be i can figure out the right syntax.

Thank you
0
Author Commented:
I figured the answer, I just put the whole calculation in a do loop till there is no more scientific notation in the result.
0

Experts Exchange Solution brought to you by

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Commented:
What about something a little more simple?  VB Script has a Round function which you can say how many decimal points to keep.  It will convert Scientific Notation into a "regular" number.

sn = 1.66666666666667E-03
d = Round(sn, 8)
MsgBox("Scientific: " & sn) ' Outputs: 1.66666666666667E-03
MsgBox("Rounded: " & d) ' Outputs: 0.00166667
0
Commented:
Don't fiddle with the value to try to get the implicit float-to-string conversion to work the way you want.  Instead, use the Format function to explicitly convert the number to a string.

The Format Function:  http://msdn.microsoft.com/en-us/library/59bz1f0h%28v=VS.90%29.aspx

MsgBox("Scientific: " & sn) ' Outputs: 1.66666666666667E-03
MsgBox("Formatted: " & Format(sn, "0.00000000") ' Outputs: 0.00166667
0
Commented:
There is also a more specialized FormatNumber library function: http://msdn.microsoft.com/en-us/library/xfta99yt%28v=VS.90%29.aspx
0
Commented:
NovaDenizen - While that would work, I would argue that your 1st approach would not be efficient, if you needed to perform further math functions on converted value later on.  Using the Round() function keeps the value a number data type.  VB Script would do all of the implicit conversions of string-to-number and vice versa pretty seemlessly, but it still a conversion.
Your 2nd approach is specific to .Net, not VB Script.
0
###### It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
VB Script

From novice to tech pro — start learning today.