Display Scientific Notation

metrick

Active Member
Licensed User
How can I convert calculated result to display in scientific notation if result is > 999999? Numberformat, NumberFormat2 have not option to display scientific notation. Could not find any function to do this. Thanks.
 

ValDog

Active Member
Licensed User
Here's a routine I developed to convert to scientific notation:


Sub Val2Sci(Value As Double, DecPlaces As Int) As String
Dim bNeg As Boolean
Dim dTemp As Double
Dim i As Int
Dim sTemp As String

If Value < 0 Then
Value = Value * -1
bNeg = True
End If

If Value > 1 Then
'divide by 10 until Value < 10; count # of times it was divided to get exponent
i = 0
Do Until Value < 10
Value = Value / 10
i = i + 1
Loop
sTemp = NumberFormat2(Value,1,DecPlaces,DecPlaces, False)
If i < 10 Then
sTemp = sTemp & "E+0" & i
Else
sTemp = sTemp & "E+" & i
End If
Else If Value < 1 Then
'multiply by 10 until Value > 1; count # of times it was multiplied to get the
' negative exponent
i = 0
Do Until Value > 1
Value = Value * 10
i = i + 1
Loop
sTemp = NumberFormat2(Value,1,DecPlaces,DecPlaces, False)
If i < 10 Then
sTemp = sTemp & "E" & "-0" & i
Else
sTemp = sTemp & "E" & "-" & i
End If
Else 'Value = 1; format with as many zeros as specifid by DecPlaces variable
sTemp = NumberFormat2(Value,1,DecPlaces,DecPlaces,False) & "E+00"
End If

If bNeg Then
'append "-" to beginning of string representation
sTemp = "-" & sTemp
End If

Return sTemp
End Sub
 
Upvote 0
Top