Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Displaying a Number in Scientific Notation 1

Status
Not open for further replies.

joelevinger

Programmer
May 13, 2003
6
US
Hello,

Using VBScript, I am trying to display a number in scientific notation if it is larger than 1 billion or smaller than 0.001. Otherwise, I want to display it in regular format, using the formatNumber function. In pseudocode, it would look like this:

If number is > 1 billion or number is < 0.001 Then
display the number using scientific notation
Else
display the number using the formatNumber function
End If

My problem is that I can't find a function to display the number using scientific notation. It turns out that if the number is >= 1 quadrillion (1,000,000,000,000,000) or smaller than 0.000000000000001, it displays in scientific notation, but otherwise it displays in regular notation. Do you have any ideas how to force it to display in scientific notation?

Any ideas would be greatly appreciated!

Joe Levinger
 
I don't know of anything built in to format values this way. When in doubt, roll your own! Seems like I knew a little math once...

SciNot.vbs
Code:
Option Explicit

Function FormatSci(ByVal floVal)
  Dim floAbsVal, intSgnVal, intScale, floScaled

  floAbsVal = Abs(floVal)
  If floAbsVal > 1e9 Or floAbsVal < 1e-3 Then
    intSgnVal = Sgn(floVal)
    intScale = Int(Log(floAbsVal) / Log(10))
    floScaled = floAbsVal / (10 ^ intScale)
    FormatSci = CStr(intSgnVal * floScaled) & &quot;e&quot; & CStr(intSCale)
  Else
    FormatSci = CStr(floVal)
  End If
End Function

Function ShowMe(ByVal floVal)
  ShowMe = CStr(floVal) & &quot; is &quot; & FormatSci(floVal)
End Function

Dim FSO, tsOut

Set FSO = CreateObject(&quot;Scripting.FileSystemObject&quot;)
Set tsOut = FSO.CreateTextFile(&quot;SciNot.txt&quot;, True)

tsOut.WriteLine ShowMe(123)
tsOut.WriteLine ShowMe(123.45)
tsOut.WriteLine ShowMe(-123)
tsOut.WriteLine ShowMe(-1234567890)
tsOut.WriteLine ShowMe(0.00012345)
tsOut.WriteLine ShowMe(0.0012345)
tsOut.WriteLine ShowMe(123456789012345.67)
tsOut.WriteLine ShowMe(-0.000000123)

tsOut.Close
Set tsOut = Nothing
Set FSO = Nothing
And the results:

SciNot.txt
Code:
123 is 123
123.45 is 123.45
-123 is -123
-1234567890 is -1.23456789e9
0.00012345 is 1.2345e-4
0.0012345 is 0.0012345
123456789012346 is 1.23456789012346e14
-0.000000123 is -1.23e-7
If this isn't what you needed maybe you could tweak it a bit to your liking.
 
Always test thoroughly before you post! ;-)

A correction:
Code:
Function FormatSci(ByVal floVal)
  Dim floAbsVal, intSgnVal, intScale, floScaled

  floAbsVal = Abs(floVal)
Code:
  If floAbsVal <> 0 And (floAbsVal > 1e9 Or floAbsVal < 1e-3) Then
Code:
    intSgnVal = Sgn(floVal)
    intScale = Int(Log(floAbsVal) / Log(10))
    floScaled = floAbsVal / (10 ^ intScale)
    FormatSci = CStr(intSgnVal * floScaled) & &quot;e&quot; & CStr(intScale)
  Else
    FormatSci = CStr(floVal)
  End If
End Function
This will work better for 0 (zero). I knew it seemed too easy.
 
I tweaked the code and got it to work for my application. Thank you so much!

Joe Levinger
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top