|
-
Sep 20th, 2000, 09:33 PM
#1
Thread Starter
Addicted Member
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...
-
Sep 20th, 2000, 09:35 PM
#2
Fanatic Member
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
-
Sep 20th, 2000, 09:38 PM
#3
Thread Starter
Addicted Member
Ah, err, mine was a bit complicated, for ex. i would input 1/2 on a textbox then the resulst in the other...
-
Sep 20th, 2000, 09:46 PM
#4
Frenzied Member
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
-
Sep 20th, 2000, 09:51 PM
#5
Thread Starter
Addicted Member
Ok, ill try it...
Thanks man...
-
Sep 20th, 2000, 09:52 PM
#6
Frenzied Member
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
-
Sep 20th, 2000, 10:00 PM
#7
Frenzied Member
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."
-
Sep 20th, 2000, 10:52 PM
#8
Junior Member
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
-
Sep 21st, 2000, 01:18 AM
#9
Lively Member
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]
-
Sep 21st, 2000, 07:14 PM
#10
Thread Starter
Addicted Member
Thanks guys, help much appreciated....
-
Sep 21st, 2000, 08:24 PM
#11
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,
-
Sep 21st, 2000, 09:27 PM
#12
_______
<?>
'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
-
Sep 21st, 2000, 10:07 PM
#13
Frenzied Member
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."
-
Sep 21st, 2000, 10:10 PM
#14
Frenzied Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|