Results 1 to 14 of 14

Thread: Math wizard anyone????

  1. #1

    Thread Starter
    Addicted Member curlywink's Avatar
    Join Date
    Mar 2000
    Location
    Manila, Philippines
    Posts
    141

    Unhappy

    Hi there,


    Can anybody help me on converting fractions to its decimals equivalent, like, 1/2 = .50 , 3/4 = .75 and so on.
    (I'm using a text box input fractions and results would be in decimals)


    Any help much appreciated. Thanks in advance...



  2. #2
    Fanatic Member Dim's Avatar
    Join Date
    Jul 2000
    Posts
    620
    If you have two text boxes one on top of the other then you can just devide the top by the bottom and have the result appear in the third box.
    Code:
    Text3.Text = Text1.Text / Text2.Text

    Hope that helps,
    D!m
    Dim

  3. #3

    Thread Starter
    Addicted Member curlywink's Avatar
    Join Date
    Mar 2000
    Location
    Manila, Philippines
    Posts
    141
    Ah, err, mine was a bit complicated, for ex. i would input 1/2 on a textbox then the resulst in the other...

  4. #4
    Frenzied Member
    Join Date
    Jun 2000
    Location
    East Providence, RI
    Posts
    1,715
    use the code that DIM gave you, if you are concernetd about the 2 text boxes instead of one, do this:

    Code:
    Private Sub Text1_Change()
        If Len(text1.Text) = 1 Then
            text2.SetFocus
        End If
    End Sub
    
    Private Sub Text2_Change()
        If Len(text3.Text) = 1 Then
            text2.SetFocus
        End If
    End Sub
    
    Private Sub Text1_GotFocus()
    text3.text = text1.text / text2.text
    End Sub
    hope thats what you need
    NXSupport - Your one-stop source for computer help

  5. #5

    Thread Starter
    Addicted Member curlywink's Avatar
    Join Date
    Mar 2000
    Location
    Manila, Philippines
    Posts
    141
    Ok, ill try it...


    Thanks man...

  6. #6
    Frenzied Member
    Join Date
    Jun 2000
    Location
    East Providence, RI
    Posts
    1,715
    2 mistakes, corrected code:

    Code:
    Private Sub Text1_Change()
        If Len(text1.Text) = 1 Then
            text2.SetFocus
        End If
    End Sub
    
    Private Sub Text2_Change()
        If Len(text2.Text) = 1 Then
            text3.SetFocus
        End If
    End Sub
    
    Private Sub Text1_GotFocus()
    text3.text = text1.text / text2.text
    End Sub
    NXSupport - Your one-stop source for computer help

  7. #7
    Frenzied Member HarryW's Avatar
    Join Date
    Jan 2000
    Location
    Heiho no michi
    Posts
    1,827
    Right, I'm pretty sure he wants this as a single string.

    Try this:

    (assuming your fraction is in a Textbox called Text1 and your decimal value appears in one called Text2)

    Code:
    Text2.Text = Val(Left(InStr(Text1.Text, "/")-1))/Val(Right(Len(Text1.Text)-InStr(Text1.Text, "/")))
    Or to make that a little more legible:

    Code:
    Dim fracVal As String
    Dim decVal As Double
    Dim slashPos as Integer
    fracVal = Text1.Text
    slashPos = InStr(fracVal, "/")
    fracVal = Val(Left(slashPos - 1) / Val(Right(Len(fracVal) - slashPos))
    Text2.Text = fracVal
    See how that does ya
    Harry.

    "From one thing, know ten thousand things."

  8. #8
    Junior Member
    Join Date
    Sep 2000
    Location
    chicago
    Posts
    26
    curlywink-- though mine is much uglier than those preceding, here's the longhand shortbrained response:
    Dim strFraction As String, aa As String
    Dim intNumerator As Integer, intDenominator As Integer
    Dim x As Integer, i As Integer
    '
    strFraction$ = Text1.Text 'make the text a string variable
    x = Len(strFraction$) 'get the total # of characters in _
    ' the fraction
    For i = 1 To x
    aa$ = Mid$(strFraction$, i, 1) 'find what's to the left and
    ' rt of the /
    If aa$ = "/" Then intNumerator = Val(Left$(strFraction$, i - 1)): intDenominator = Val(Right$(strFraction$, x - i))
    Next i


    good luck

  9. #9
    Lively Member
    Join Date
    Feb 2000
    Posts
    118

    a little tweak

    Put a little tweak on HarryW's code.
    Code:
    Private Sub Command1_Click()
    Dim fracVal As String
    Dim decVal As Double
    Dim slashPos As Integer
    fracVal = Text1.Text
    slashPos = InStr(fracVal, "/")
    decVal = Val(Left(fracVal, slashPos - 1)) / Val(Right(fracVal, slashPos))
    Text2.Text = decVal
    End Sub
    [Edited by kokopeli on 09-21-2000 at 02:31 AM]
    Kokopeli
    VB6 SP3

  10. #10

    Thread Starter
    Addicted Member curlywink's Avatar
    Join Date
    Mar 2000
    Location
    Manila, Philippines
    Posts
    141

    Talking

    Thanks guys, help much appreciated....

  11. #11
    Guest
    and I put a little tweek on kokopeli's code

    Code:
    Private Sub Command1_Click()
    
      Dim fracVal As String
      Dim decVal As Double, dNum As Double, dDev As Double
      Dim slashPos As Integer
      
      fracVal = Trim(Text1.Text)
      slashPos = InStr(fracVal, "/")
      
      dNum = CDbl(Left$(fracVal, slashPos - 1))
      dDev = CDbl(Mid(fracVal, slashPos + 1, Len(fracVal) - slashPos))
      
      If dNum <> 0 And dDev <> 0 Then
        decVal = dNum / dDev
        Text2.Text = decVal
      Else
        Text2.Text = "Can not divied by Zero"
      End If
        
    End Sub
    Best,

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

    <?>

    'if the fractions are entered in code
    ie. text1= 1/2 then use that and not text1 = "1/2"

    Text3 = Val(Text1) / Val(Text2)
    "A myth is not the succession of individual images,
    but an integerated meaningful entity,
    reflecting a distinct aspect of the real world."

    ___ Adolf Jensen

  13. #13
    Frenzied Member HarryW's Avatar
    Join Date
    Jan 2000
    Location
    Heiho no michi
    Posts
    1,827
    I'm fairly sure that curlywink said he wanted to use 2 textboxes, one for input and one for output.
    Harry.

    "From one thing, know ten thousand things."

  14. #14
    Frenzied Member
    Join Date
    Jun 2000
    Location
    East Providence, RI
    Posts
    1,715
    Originally posted by curlywink
    Ah, err, mine was a bit complicated, for ex. i would input 1/2 on a textbox then the resulst in the other...
    Here cutly wink says that he wants the input in one text box, and the output in another
    NXSupport - Your one-stop source for computer help

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