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...
Printable View
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...
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
Ah, err, mine was a bit complicated, for ex. i would input 1/2 on a textbox then the resulst in the other...
use the code that DIM gave you, if you are concernetd about the 2 text boxes instead of one, do this:
hope thats what you needCode: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
Ok, ill try it...
Thanks man...
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
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)
Or to make that a little more legible:Code:Text2.Text = Val(Left(InStr(Text1.Text, "/")-1))/Val(Right(Len(Text1.Text)-InStr(Text1.Text, "/")))
See how that does ya :)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
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
Put a little tweak on HarryW's code.
[Edited by kokopeli on 09-21-2000 at 02:31 AM]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
Thanks guys, help much appreciated....
and I put a little tweek on kokopeli's code :)
Best,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
'if the fractions are entered in code
ie. text1= 1/2 then use that and not text1 = "1/2"
Text3 = Val(Text1) / Val(Text2)
I'm fairly sure that curlywink said he wanted to use 2 textboxes, one for input and one for output.
Here cutly wink says that he wants the input in one text box, and the output in anotherQuote:
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...