|
-
Sep 12th, 2007, 03:05 AM
#1
Thread Starter
Junior Member
VB Calculator, Decimal Places...
Hey guys, i have come across a dilemma...
i am unsure of how to get my answers to two decimal places...i can easily get it into full integer but it HAS to give all answer in 2 decimal places...
i.e. 3.123 + 3.516 = 6.64
Also if anyone can give me any pointers on the rest of my code that would be great 
Code:
Dim op As String
Option Explicit
Dim value1 As Double
Dim value2 As Double
Dim Result As Double
Dim calccount As Integer
Private Sub Command1_Click(Index As Integer)
If calccount = 0 Then
Text1.Text = " "
MsgBox ("Calculator is not on.")
End If
If calccount = 1 Then
Text1.Text = " "
calccount = calccount + 1
End If
If calccount > 1 Then
Text1.Text = Text1.Text & command1(Index).Caption
End If
End Sub
Private Sub Command10_Click()
Text1.Text = mem
End Sub
Private Sub Command3_Click()
If calccount > 0 Then
value2 = Val(Text1.Text)
Select Case (op)
Case "+"
Result = value1 + value2
Text1.Text = Result
calccount = 0
Case "-"
Result = value1 - value2
Text1.Text = Result
calccount = 0
Case "*"
Result = value1 * value2
Text1.Text = Result
calccount = 0
Case "/"
Result = value1 / value2
Text1.Text = Result
calccount = 0
End Select
End If
End Sub
Private Sub Command4_Click(Index As Integer)
Result = value1
value1 = Result + Val(Text1.Text)
Text1.Text = " "
op = Command4(Index).Caption
End Sub
Private Sub Command5_Click()
Result = 0
value1 = 0
value2 = 0
Text1.Text = "0"
calccount = 1
End Sub
Private Sub Command6_Click()
calccount = 0
Text1.Text = "0"
End Sub
Private Sub Command7_Click()
Result = 0
value1 = 0
value2 = 0
calccount = 1
Text1.Text = "0"
End Sub
Thanks,
FL.
-
Sep 12th, 2007, 04:11 AM
#2
Addicted Member
Re: VB Calculator, Decimal Places...
You can try this:
Code:
Text1.Text=Format(Text1.Text, ".##")
Do Good. Be Good. The World is yours.
-
Sep 12th, 2007, 04:45 AM
#3
Thread Starter
Junior Member
Re: VB Calculator, Decimal Places...
Yo,
i tried...
vb Code:
Dim op As String
Option Explicit
Dim value1 As Double
Dim value2 As Double
Dim Result As Double
Dim calccount As Integer
Private Sub Command1_Click(Index As Integer)
If calccount = 0 Then
Text1.Text = " "
MsgBox ("Calculator is not on.")
End If
If calccount = 1 Then
Text1.Text = " "
calccount = calccount + 1
End If
If calccount > 1 Then
Text1.Text = Text1.Text & command1(Index).Caption
End If
End Sub
Private Sub Command10_Click()
Text1.Text = mem
End Sub
Private Sub Command3_Click()
If calccount > 0 Then
value2 = Val(Text1.Text)
Select Case (op)
Case "+"
Result = value1 + value2
Text1.Text = Result
calccount = 0
Case "-"
Result = value1 - value2
Text1.Text = Result
calccount = 0
Case "*"
Result = value1 * value2
Text1.Text = Result
calccount = 0
Case "/"
Result = value1 / value2
Text1.Text = Result
calccount = 0
End Select
End If
End Sub
Private Sub Command4_Click(Index As Integer)
Result = value1
value1 = Result + Val(Text1.Text)
Text1.Text = " "
op = Command4(Index).Caption
End Sub
Private Sub Command5_Click()
Result = 0
value1 = 0
value2 = 0
Text1.Text = "0"
calccount = 1
End Sub
Private Sub Command6_Click()
calccount = 0
Text1.Text = ""
End Sub
Private Sub Command7_Click()
Result = 0
value1 = 0
value2 = 0
calccount = 1
Text1.Text = "0"
End Sub
Private Sub Text1_Change()
Text1.Text = Format(Text1.Text, ".##")
End Sub
and it kept reseting the decimal count in a weird way...
i then tried...
vb Code:
Dim op As String
Option Explicit
Dim value1 As Double
Dim value2 As Double
Dim Result As Double
Dim calccount As Integer
Private Sub Command1_Click(Index As Integer)
If calccount = 0 Then
Text1.Text = " "
MsgBox ("Calculator is not on.")
End If
If calccount = 1 Then
Text1.Text = " "
calccount = calccount + 1
End If
If calccount > 1 Then
Text1.Text = Text1.Text & command1(Index).Caption
End If
End Sub
Private Sub Command10_Click()
Text1.Text = mem
End Sub
Private Sub Command3_Click()
If calccount > 0 Then
value2 = Val(Text1.Text)
Select Case (op)
Case "+"
Result = value1 + value2
Text1.Text = Result
calccount = 0
Case "-"
Result = value1 - value2
Text1.Text = Result
calccount = 0
Case "*"
Result = value1 * value2
Text1.Text = Result
calccount = 0
Case "/"
Result = value1 / value2
Text1.Text = Result
calccount = 0
End Select
End If
End Sub
Private Sub Command4_Click(Index As Integer)
Result = value1
value1 = Result + Val(Text1.Text)
Text1.Text = Format(Text1.Text, ".##")
op = Command4(Index).Caption
End Sub
Private Sub Command5_Click()
Result = 0
value1 = 0
value2 = 0
Text1.Text = "0"
calccount = 1
End Sub
Private Sub Command6_Click()
calccount = 0
Text1.Text = ""
End Sub
Private Sub Command7_Click()
Result = 0
value1 = 0
value2 = 0
calccount = 1
Text1.Text = "0"
End Sub
and when i hit a command button it would then go onto the end of value 1?
e.g. 1 + 111 = 1.111
I'm obviously putting it in the wrong place!
Can you please tell me, i'm very very nooby as i only started today with a basis of a calculator...
FL
-
Sep 12th, 2007, 04:53 AM
#4
Re: VB Calculator, Decimal Places...
as this is a calculater you should use the round function to set the number of decimal places
vb Code:
?round(457.885258,2)
457.89
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
-
Sep 12th, 2007, 04:54 AM
#5
Addicted Member
Re: VB Calculator, Decimal Places...
i guess you are calculating the value on a click of button
then the following code is fine
Code:
Format(CDbl(Text1.Text), ".##")
Do Good. Be Good. The World is yours.
-
Sep 12th, 2007, 06:41 AM
#6
Thread Starter
Junior Member
Re: VB Calculator, Decimal Places...
 Originally Posted by westconn1
as this is a calculater you should use the round function to set the number of decimal places
vb Code:
?round(457.885258,2)
457.89
I'm just going by what the assignment says, sorry i should of said earlier it says i can't add any other functions bar +,-,/,* or i'll be heavily marked down...and all answers MUST be 2 decimal places...i'll try that code just above this post...i guess i just replaced the resault value righttt???
FL
-
Sep 12th, 2007, 06:44 AM
#7
Thread Starter
Junior Member
Re: VB Calculator, Decimal Places...
 Originally Posted by vksingh24
i guess you are calculating the value on a click of button
then the following code is fine
Code:
Format(CDbl(Text1.Text), ".##")
Code:
Dim op As String
Option Explicit
Dim value1 As Double
Dim value2 As Double
Dim Result As Double
Dim calccount As Integer
Private Sub Command1_Click(Index As Integer)
If calccount = 0 Then
Text1.Text = " "
MsgBox ("Calculator is not on.")
End If
If calccount = 1 Then
Text1.Text = " "
calccount = calccount + 1
End If
If calccount > 1 Then
Text1.Text = Text1.Text & command1(Index).Caption
End If
End Sub
Private Sub Command10_Click()
Text1.Text = mem
End Sub
Private Sub Command3_Click()
If calccount > 0 Then
value2 = Val(Text1.Text)
Select Case (op)
Case "+"
Result = value1 + value2
Text1.Text = Result
calccount = 0
Case "-"
Result = value1 - value2
Text1.Text = Result
calccount = 0
Case "*"
Result = value1 * value2
Text1.Text = Result
calccount = 0
Case "/"
Result = value1 / value2
Text1.Text = Result
calccount = 0
End Select
End If
End Sub
Private Sub Command4_Click(Index As Integer)
Result = value1
value1 = Result + Val(Text1.Text)
Text1.Text = Format(CDbl(Text1.Text), ".##")
op = Command4(Index).Caption
End Sub
Private Sub Command5_Click()
Result = 0
value1 = 0
value2 = 0
Text1.Text = "0"
calccount = 1
End Sub
Private Sub Command6_Click()
calccount = 0
Text1.Text = ""
End Sub
Private Sub Command7_Click()
Result = 0
value1 = 0
value2 = 0
calccount = 1
Text1.Text = "0"
End Sub
i used that and when eva i hit a function button it adds the decimal?
like
Press 3...'displays 3..
then press + 'display 3.
???
FL
-
Sep 12th, 2007, 07:25 AM
#8
Re: VB Calculator, Decimal Places...
ok to without using any other functions multipy * 100 as a long then divide by 100
try this
?(155.3565656*100\1)/100
155.36
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
-
Sep 12th, 2007, 07:43 AM
#9
Addicted Member
Re: VB Calculator, Decimal Places...
Here is how your code needs to work to get your desired result.
Dim MyValue as Double
MyValue = 123.456 ' Initial value
MyValue = Int((MyValue * 100) + .5 ' Adds 2 numeric places and rounds it off
Myvalue = MyValue / 100 ' Now all decimals over 2 have been removed
This will not work for decimals that end in a ) like 123.40. This would still show as 123.4. For this to remain true without using any functions other than math functions this will need to be turned into a string
Dim MyValue as Double
Dim MyString as String
MyValue = 123.456
MyValue = int((MyValue * 100) + .5 ' Adds 2 numeric places and rounds it off
MyString = Left(Str$(MyValue, Len(Str$(MyValue)-2))) + "." + Right(Str$(Myvalue,2))
I'm not sure if this is what you are looking for but this is basically how some of the functions that do this for you do the conversion. Hopefully it will at least help you figure it out.
-
Sep 12th, 2007, 08:16 AM
#10
Addicted Member
Re: VB Calculator, Decimal Places...
 Originally Posted by westconn1
ok to without using any other functions multipy * 100 as a long then divide by 100
try this
?(155.3565656*100\1)/100
155.36
Sorry westconn, somehow I didn't see your post before posting mine.
-
Sep 12th, 2007, 08:49 AM
#11
Thread Starter
Junior Member
Re: VB Calculator, Decimal Places...
 Originally Posted by westconn1
ok to without using any other functions multipy * 100 as a long then divide by 100
try this
?(155.3565656*100\1)/100
155.36
Where exactly do i put this?
Because that just seems like an eqn to be?
i will try the string values as well...
-
Sep 12th, 2007, 10:23 PM
#12
Re: VB Calculator, Decimal Places...
you put the code where ever you want to return a 2 digit only result, the best way to do it is declare a long variable, and set it's value to 100
vb Code:
dim centum as long
centum = 100
myresult = (myresult * centum \1)/centum)
if you declare centum at the top of the form in the general section it will be available all through your program, set its value in form open event
Because that just seems like an eqn to be?
it is a mathematical calculation
dividing with the \ symbol returns a whole number
so multiply any number by 100, then \1 gives you a whole number that divided by 100 will give up to 2 decimal places (if there is any decimal part)
it says i can't add any other functions bar +,-,/,* or
as it is a calculator you should avoid converting to strings, which are other functions anyway
if you don't like to use the \ divider you can use a long variable to store the value in between
vb Code:
Dim r as long
r = myresult * centum
myresult = r / centum
again r can be declared in the general section then it is available all through your program
Last edited by westconn1; Sep 12th, 2007 at 10:29 PM.
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
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
|