Hi, I have a text box which shows the result of a
calculationcan anyone tell me how to format this text box
to show scientific notation.
And also how do i set the number of decimal places for this
notation.
thanks in advance
MICK
Printable View
Hi, I have a text box which shows the result of a
calculationcan anyone tell me how to format this text box
to show scientific notation.
And also how do i set the number of decimal places for this
notation.
thanks in advance
MICK
Have you tried looking at the property of the textbox DataFormat?
Hi Nitro, thanks for the reply.
Is there a way to change this propery at run time using code
what i want to do is have two option buttons, clicking on
one will make the text box display a number using decimal notation, and clicking on the other will display it in
scientific notation. I want them both to use three decimal places.
thanks again
MICK
Hello Bucko!
Try something like this Bucko. Place a textbox on your form and change the DataFormat to whatever. Place the following in the form_load and you should get the hang of it from there.
Code:MsgBox Text1.DataFormat.Format
How about
Text1 = Format(NumberVar, "Scientific")
Roger
If you have VB6, you can set the number of decimals using the Round function.
Code:'Rounds to 1 decimal place
RetVal = Round(MyNum, 1)
if not you can round them this way...
'rounding a number to 2 decimal places
Function RoundNumber(lNumber, Optional iDecimalPlaces As Integer = 1)
RoundNumber = Int(lNumber * (10 ^ iDecimalPlaces) + 0.5) / (10 ^ iDecimalPlaces)
End Function
Here is a small code I slapped together. It will return a String in the form of 1.11 x 10^5. If the number is less than 1 (in decimals) it will return a negative exponent. 1.11 x 10^-5
Code:Function CSci(Number)
If IsNumeric(Number) Then
If Number >= 1 Then
tmpNum = CStr(Number)
GetPosOfZeros = InStr(1, tmpNum, "0") - 1
NumOfZeros = Right(tmpNum, Len(tmpNum) - GetPosOfZeros)
chkNum = CDbl(NumOfZeros)
If chkNum <> 0 Then
For I = 1 To Len(NumOfZeros)
NumOfZeros = Right(tmpNum, Len(tmpNum) - GetPosOfZeros - I)
chkNum = CDbl(NumOfZeros)
If chkNum = 0 Then Exit For
Next I
End If
nExp = Len(NumOfZeros)
nNum = Left(tmpNum, Len(tmpNum) - nExp)
tmp = CDbl(nNum)
Do While tmp > 10
tmp = tmp / 10
Loop
newnum = CStr(tmp)
AddZeros = Len(Right(newnum, Len(newnum) - 3))
CSci = newnum & " x " & " 10^" & nExp + AddZeros
ElseIf Number < 1 Then
tmpNum = CStr(Number)
tmpNum = Right(tmpNum, Len(tmpNum) - 2)
GetZeroPos = InStrRev(tmpNum, "0")
NumOfZeros = Left(tmpNum, GetZeroPos)
chkNum = CDbl(NumOfZeros)
If chkNum <> 0 Then
For I = 1 To Len(NumOfZeros)
NumOfZeros = Left(tmpNum, Len(tmpNum) - GetZerosPos - I)
chkNum = CDbl(NumOfZeros)
If chkNum = 0 Then Exit For
Next I
End If
nExp = Len(NumOfZeros)
nNum = Right(tmpNum, Len(tmpNum) - NumOfZeros)
tmp = CDbl(nNum)
Do While tmp > 10
tmp = tmp / 10
Loop
newnum = CStr(tmp)
CSci = newnum & " x " & "10^-" & nExp + 2
End If
End If
End Function
The usage is basically like CInt, CLng, CStr etc. Make a Form with a TextBox and place the following code into a CommandButton. It will return and print whatever you wrote in the TextBox in Scientific Notation.
[Edited by Megatron on 07-05-2000 at 07:16 PM]Code:Private Sub Command1_Click()
RetVal = CSci(Text1)
Print RetVal
End Sub