Results 1 to 12 of 12

Thread: VB Calculator, Decimal Places...

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Sep 2007
    Posts
    22

    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.

  2. #2
    Addicted Member
    Join Date
    Aug 2007
    Location
    India
    Posts
    141

    Re: VB Calculator, Decimal Places...

    You can try this:

    Code:
    Text1.Text=Format(Text1.Text, ".##")
    Do Good. Be Good. The World is yours.

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Sep 2007
    Posts
    22

    Re: VB Calculator, Decimal Places...

    Yo,

    i tried...

    vb Code:
    1. Dim op As String
    2. Option Explicit
    3.  
    4. Dim value1 As Double
    5. Dim value2 As Double
    6. Dim Result As Double
    7. Dim calccount As Integer
    8.  
    9.  
    10.  
    11. Private Sub Command1_Click(Index As Integer)
    12.  
    13. If calccount = 0 Then
    14. Text1.Text = " "
    15. MsgBox ("Calculator is not on.")
    16.  
    17. End If
    18.  
    19. If calccount = 1 Then
    20. Text1.Text = " "
    21. calccount = calccount + 1
    22. End If
    23.  
    24. If calccount > 1 Then
    25. Text1.Text = Text1.Text & command1(Index).Caption
    26. End If
    27.  
    28. End Sub
    29.  
    30. Private Sub Command10_Click()
    31.  
    32.  Text1.Text = mem
    33. End Sub
    34.  
    35.  
    36.  
    37. Private Sub Command3_Click()
    38. If calccount > 0 Then
    39. value2 = Val(Text1.Text)
    40. Select Case (op)
    41. Case "+"
    42.         Result = value1 + value2
    43.         Text1.Text = Result
    44.         calccount = 0
    45. Case "-"
    46.         Result = value1 - value2
    47.         Text1.Text = Result
    48.         calccount = 0
    49. Case "*"
    50.         Result = value1 * value2
    51.         Text1.Text = Result
    52.         calccount = 0
    53. Case "/"
    54.         Result = value1 / value2
    55.         Text1.Text = Result
    56.         calccount = 0
    57. End Select
    58. End If
    59. End Sub
    60.  
    61. Private Sub Command4_Click(Index As Integer)
    62. Result = value1
    63. value1 = Result + Val(Text1.Text)
    64. Text1.Text = " "
    65.  
    66. op = Command4(Index).Caption
    67. End Sub
    68.  
    69. Private Sub Command5_Click()
    70. Result = 0
    71. value1 = 0
    72. value2 = 0
    73. Text1.Text = "0"
    74. calccount = 1
    75. End Sub
    76.  
    77. Private Sub Command6_Click()
    78. calccount = 0
    79. Text1.Text = ""
    80. End Sub
    81.  
    82. Private Sub Command7_Click()
    83. Result = 0
    84. value1 = 0
    85. value2 = 0
    86. calccount = 1
    87. Text1.Text = "0"
    88. End Sub
    89.  
    90.  
    91. Private Sub Text1_Change()
    92. Text1.Text = Format(Text1.Text, ".##")
    93. End Sub

    and it kept reseting the decimal count in a weird way...

    i then tried...

    vb Code:
    1. Dim op As String
    2. Option Explicit
    3.  
    4. Dim value1 As Double
    5. Dim value2 As Double
    6. Dim Result As Double
    7. Dim calccount As Integer
    8.  
    9.  
    10.  
    11. Private Sub Command1_Click(Index As Integer)
    12.  
    13. If calccount = 0 Then
    14. Text1.Text = " "
    15. MsgBox ("Calculator is not on.")
    16.  
    17. End If
    18.  
    19. If calccount = 1 Then
    20. Text1.Text = " "
    21. calccount = calccount + 1
    22. End If
    23.  
    24. If calccount > 1 Then
    25. Text1.Text = Text1.Text & command1(Index).Caption
    26. End If
    27.  
    28. End Sub
    29.  
    30. Private Sub Command10_Click()
    31.  
    32.  Text1.Text = mem
    33. End Sub
    34.  
    35.  
    36.  
    37. Private Sub Command3_Click()
    38. If calccount > 0 Then
    39. value2 = Val(Text1.Text)
    40. Select Case (op)
    41. Case "+"
    42.         Result = value1 + value2
    43.         Text1.Text = Result
    44.         calccount = 0
    45. Case "-"
    46.         Result = value1 - value2
    47.         Text1.Text = Result
    48.         calccount = 0
    49. Case "*"
    50.         Result = value1 * value2
    51.         Text1.Text = Result
    52.         calccount = 0
    53. Case "/"
    54.         Result = value1 / value2
    55.         Text1.Text = Result
    56.         calccount = 0
    57. End Select
    58. End If
    59. End Sub
    60.  
    61. Private Sub Command4_Click(Index As Integer)
    62. Result = value1
    63. value1 = Result + Val(Text1.Text)
    64. Text1.Text = Format(Text1.Text, ".##")
    65.  
    66. op = Command4(Index).Caption
    67. End Sub
    68.  
    69. Private Sub Command5_Click()
    70. Result = 0
    71. value1 = 0
    72. value2 = 0
    73. Text1.Text = "0"
    74. calccount = 1
    75. End Sub
    76.  
    77. Private Sub Command6_Click()
    78. calccount = 0
    79. Text1.Text = ""
    80. End Sub
    81.  
    82. Private Sub Command7_Click()
    83. Result = 0
    84. value1 = 0
    85. value2 = 0
    86. calccount = 1
    87. Text1.Text = "0"
    88. 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

  4. #4
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    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:
    1. ?round(457.885258,2)
    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

  5. #5
    Addicted Member
    Join Date
    Aug 2007
    Location
    India
    Posts
    141

    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.

  6. #6

    Thread Starter
    Junior Member
    Join Date
    Sep 2007
    Posts
    22

    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:
    1. ?round(457.885258,2)
    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

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Sep 2007
    Posts
    22

    Smile 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

  8. #8
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    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

  9. #9
    Addicted Member
    Join Date
    Mar 2007
    Posts
    203

    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.

  10. #10
    Addicted Member
    Join Date
    Mar 2007
    Posts
    203

    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.

  11. #11

    Thread Starter
    Junior Member
    Join Date
    Sep 2007
    Posts
    22

    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...

  12. #12
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    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:
    1. dim centum as long
    2. centum = 100
    3.  
    4. 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:
    1. Dim r as long
    2. r = myresult * centum
    3. 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
  •  



Click Here to Expand Forum to Full Width