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.
Re: VB Calculator, Decimal Places...
You can try this:
Code:
Text1.Text=Format(Text1.Text, ".##")
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
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
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), ".##")
Re: VB Calculator, Decimal Places...
Quote:
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
Re: VB Calculator, Decimal Places...
Quote:
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
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
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.
Re: VB Calculator, Decimal Places...
Quote:
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.
Re: VB Calculator, Decimal Places...
Quote:
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...
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
Quote:
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)
Quote:
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