Hi, I have made a basic calculator program, but need help with some minor things. I have created two radio buttons for the user to click on. If the first is clicked/selected, then the output would be formatted in floating point value (radFloat). If the second is selected, then the answer should be formatted to two decimal places(radTwoDecimal). As of now, i have the calculator working fine, I just can't come up with how to solve the decimal output problem.

I have tried something like this for the first button(but i know its wrong):

If radFloat.checked = true Then
    Total = Ctype(total, double)
else
    total = ctype(total, integer)
end if

My code thus far is:

Public Class frmCalc
    Dim num1 As Integer
    Dim num2 As Integer
    Dim mathSymbol As String
    Dim Total As Double

    Private Sub btn1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn1.Click
        txtEnter.Text &= CInt(btn1.Text)
    End Sub

    Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click
        txtEnter.Clear()
        txtEnter.Focus()
    End Sub

    Private Sub btn0_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn0.Click
        txtEnter.Text &= CInt(btn0.Text)
    End Sub

    Private Sub btn2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn2.Click
        txtEnter.Text &= CInt(btn2.Text)
    End Sub

    Private Sub btn3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn3.Click
        txtEnter.Text &= CInt(btn3.Text)
    End Sub

    Private Sub btn6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn6.Click
        txtEnter.Text &= CInt(btn6.Text)
    End Sub

    Private Sub btn5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn5.Click
        txtEnter.Text &= CInt(btn5.Text)
    End Sub

    Private Sub btn4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn4.Click
        txtEnter.Text &= CInt(btn4.Text)
    End Sub

    Private Sub btn7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn7.Click
        txtEnter.Text &= CInt(btn7.Text)
    End Sub

    Private Sub btn8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn8.Click
        txtEnter.Text &= CInt(btn8.Text)
    End Sub

    Private Sub btn9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn9.Click
        txtEnter.Text &= CInt(btn9.Text)
    End Sub

    Private Sub btnPeriod_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPeriod.Click

        If txtEnter.Text.Length >= 1 Then
            txtEnter.Text &= btnPeriod.Text
        Else
            Exit Sub
        End If

    End Sub

    Private Sub chkBasic_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkBasic.CheckedChanged
        If chkBasic.Checked = True Then
            btnMultiply.Hide()
            btnDivide.Hide()
        Else
            btnMultiply.Show()
            btnDivide.Show()
        End If
    End Sub

    Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
        num1 = CInt(txtEnter.Text)

        txtEnter.Clear()

        mathSymbol = btnAdd.Text
    End Sub

    Private Sub btnMultiply_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMultiply.Click
        num1 = CInt(txtEnter.Text)

        txtEnter.Clear()

        mathSymbol = btnMultiply.Text
    End Sub

    Private Sub btnDivide_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDivide.Click
        num1 = CInt(txtEnter.Text)

        txtEnter.Clear()

        mathSymbol = btnDivide.Text
    End Sub

    Private Sub btnSubtract_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSubtract.Click

        num1 = CInt(txtEnter.Text)

        txtEnter.Clear()

        mathSymbol = btnSubtract.Text
    End Sub

    Private Sub btnEquals_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEquals.Click
        num2 = CInt(txtEnter.Text)

        txtEnter.Text = num2

        DoMath()

    End Sub

    Sub DoMath()

        If mathSymbol = "+" Then
            Total = num1 + num2
        End If
        txtEnter.Text = Total

        If mathSymbol = "-" Then
            Total = num1 - num2
        End If
        txtEnter.Text = Total

        If mathSymbol = "*" Then
            Total = num1 * num2
        End If
        txtEnter.Text = Total

        If mathSymbol = "/" Then
            If num2 = CInt("0") Then
                MsgBox("You CANNOT divide by 0!", 0, "Error")
                Exit Sub
            End If
            Total = num1 / num2
        End If
        txtEnter.Text = Total

    End Sub
End Class

Any help?

Member Avatar for Unhnd_Exception

It looks good to me

try

If radFloat.checked Then   
  Total = math.round(total,2)
else
    total = cint(total)
end if
Member Avatar for Unhnd_Exception

If your looking for 1 with more decimals and 1 with 2 then

If radFloat.checked Then   
  Total = math.round(total,9)
else
    total = math.round(total,2)
end if

Do i need to put that code for each radio button under the equals button sum function or the DoMath() sub routine? Keep in mind that the answer should be formatted as a decimal number (float) or to 2 decimals

write the codes under the 'checked' procedure of both radio buttons

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.