Results 1 to 8 of 8

Thread: Formating a text box

  1. #1

    Thread Starter
    Member
    Join Date
    Feb 1999
    Posts
    34
    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


  2. #2
    Fanatic Member
    Join Date
    Jan 2000
    Location
    Nitro
    Posts
    633
    Have you tried looking at the property of the textbox DataFormat?
    Chemically Formulated As:
    Dr. Nitro

  3. #3

    Thread Starter
    Member
    Join Date
    Feb 1999
    Posts
    34
    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

  4. #4
    Fanatic Member
    Join Date
    Jan 2000
    Location
    Nitro
    Posts
    633
    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
    Chemically Formulated As:
    Dr. Nitro

  5. #5
    Lively Member
    Join Date
    Dec 1999
    Location
    Karlsruhe, Germany
    Posts
    122
    How about

    Text1 = Format(NumberVar, "Scientific")

    Roger

  6. #6
    Guest
    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)

  7. #7
    _______ HeSaidJoe's Avatar
    Join Date
    Jun 1999
    Location
    Canada
    Posts
    3,946

    <?>

    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
    "A myth is not the succession of individual images,
    but an integerated meaningful entity,
    reflecting a distinct aspect of the real world."

    ___ Adolf Jensen

  8. #8
    Guest
    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.

    Code:
    Private Sub Command1_Click()
    
        RetVal = CSci(Text1)
        Print RetVal
        
    End Sub
    [Edited by Megatron on 07-05-2000 at 07:16 PM]

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width