• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 3459
  • Last Modified:

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?
0
IISB
Asked:
IISB
  • 3
  • 2
  • 2
  • +3
1 Solution
 
Patrick MatthewsCommented:
Please post your code
0
 
aburrCommented:
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
 
IISBAuthor 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
Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

 
Saqib Husain, SyedEngineerCommented:
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
 
IISBAuthor 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
 
IISBAuthor 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
 
dlmichenerCommented:
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
 
NovaDenizenCommented:
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
 
NovaDenizenCommented:
There is also a more specialized FormatNumber library function: http://msdn.microsoft.com/en-us/library/xfta99yt%28v=VS.90%29.aspx
0
 
dlmichenerCommented:
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

Featured Post

The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

  • 3
  • 2
  • 2
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now