-1

I'm Taking a visual basic class at my community college and my teacher can't seem to answer any of my questions, i could really use some help with this project.

I've got it so far but im having trouble formatting currency.

I've tried using variables instead of the txtbox.text

Public Class Payroll
    Dim count As Integer = 0
    Const federal_tax_rate As Decimal = 0.12
    Const state_tax_rate As Decimal = 0.04
    Const medicare_tax_rate As Decimal = 0.01
    Dim GrossPay = txtGrossPay.Text.ToString("C")
    Dim FederalTax.ToString("C") = txtFederalTax.text
    Dim StateTax.tostring("C") = txtStateTax.text
    Dim MedicareTax.tostring("C") = txtMedicareTax.text
    Dim NetPay.tostring("C") = txtNetPay.text



    Private Sub btnCalculate_Click(sender As Object, e As EventArgs) Handles btnCalculate.Click
        GrossPay = txtHoursWorked.Text * txtPayRate.Text
        FederalTax = federal_tax_rate * txtGrossPay.Text
        StateTax = state_tax_rate * txtGrossPay.Text
        MedicareTax = medicare_tax_rate * txtGrossPay.Text
        NetPay = txtGrossPay.Text - txtFederalTax.Text - txtStateTax.Text - txtMedicareTax.Text
    End Sub

    'Closes the program.
    Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click
        Me.Close()
    End Sub

    Private Sub btnDisplayOutput_Click(sender As Object, e As EventArgs) Handles btnDisplayOutput.Click
        count = count + 1
        lstOutput.Items.Add("Employee " & count)
        lstOutput.Items.Add("----------------")
        lstOutput.Items.Add("Name: ")
        lstOutput.Items.Add("Social Security Number: ")
        lstOutput.Items.Add("Department: ")
        lstOutput.Items.Add("Hours worked: " & txtHoursWorked.Text)
        lstOutput.Items.Add("Pay rate: " & txtHoursWorked.Text)
        lstOutput.Items.Add("Gross pay: " & txtGrossPay.Text)
        lstOutput.Items.Add("Federal tax withheld: " & txtFederalTax.Text)
        lstOutput.Items.Add("State tax withheld: " & txtStateTax.Text)
        lstOutput.Items.Add("Medicare tax withheld: " & txtMedicareTax.Text)
        lstOutput.Items.Add("Net pay: " & txtNetPay.Text)
    End Sub

    'Clears the output box.
    Private Sub btnReset_Click(sender As Object, e As EventArgs) Handles btnReset.Click
        txtHoursWorked.Text = ""
        txtPayRate.Text = ""

        lstOutput.Items.Clear()
    End Sub
End Class
Ňɏssa Pøngjǣrdenlarp
  • 38,411
  • 12
  • 59
  • 178
  • 4
    Please read [ask] and take the [tour]. This is not a tutorial site. If you ask a specific question, it is easier for someone to answer. Start by setting `Option Strict On` – Ňɏssa Pøngjǣrdenlarp Sep 29 '17 at 22:52

2 Answers2

0

You could always declare all your variables as Single, and then when outputting results, use a format statement which can control the decimal precision, like:

lstOutput.Items.Add("Gross pay: " & Format(GrossPay,"0.00"))

But you would need to get rid of all the "txt" prefixes of your output variables. For numerical variables related to financial amounts, I would never store cost values via text fields, but rather keep everything in Single type, and then format the output statements. (in other words, you don't have to use Decimal or Currency). If rounding errors became important, you could transition to double-precision Double type variables.

  • This is bad advice; decimal types should always be used for currency. See for one of many many examples, [this SO answer](https://stackoverflow.com/questions/1165761/decimal-vs-double-which-one-should-i-use-and-when) – peterG Sep 30 '17 at 14:05
  • That's a great comment. If you wanted to use an alternative to Decimal, instead of Single, you would really have to use Double to best approximate the integer limits of Decimal. –  Sep 30 '17 at 15:21
-1

.tostring("C") has to be on the right side of the equals sign

Dim NetPay as string = txtNetPay.text.tostring("C2")

But more correct than having the variable as string would be to set the variable as data type Single, and doing a .tostring("C") when you display the value

Dim NetPay as single = csng(txtnetpay.text)

lstOutput.Items.Add("Net pay: " & NetPay.Tostring("C2")

Hope this helps

Davester
  • 14
  • 4
  • Something has to be seriously wrong with my code, I tried both suggestions and there was no difference in the program. – Savannah Roberts Sep 29 '17 at 23:07
  • lstOutput.Items.Add("Medicare tax withheld: " & NetPay.tostring("C2")) Be sure the NetPay variable is a data type single – Davester Sep 29 '17 at 23:45