工资项目

时间:2017-09-29 22:48:36

标签: vb.net

我在社区大学上进行视觉基础课,而我的老师似乎无法回答我的任何问题,我真的可以在这个项目中使用一些帮助。

我到目前为止已经得到了它,但我在格式化货币方面遇到了麻烦。

我尝试过使用变量代替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

2 个答案:

答案 0 :(得分:0)

您始终可以将所有变量声明为Single,然后在输出结果时,使用可控制小数精度的format语句,如:

lstOutput.Items.Add(“总薪酬:”和格式(GrossPay,“0.00”))

但是你需要摆脱输出变量的所有“txt”前缀。对于与财务金额相关的数值变量,我永远不会通过文本字段存储成本值,而是将所有内容保存为单一类型,然后格式化输出语句。 (换句话说,您不必使用十进制或货币)。如果舍入错误变得很重要,您可以转换为双精度Double类型变量。

答案 1 :(得分:-1)

.tostring(" C")必须位于等号的右侧

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

但是将变量作为字符串更正确的方法是将变量设置为数据类型Single,并在显示值时执行.tostring(" C")

Dim NetPay为single = csng(txtnetpay.text)

lstOutput.Items.Add("净支付:"& NetPay.Tostring(" C2")

希望这有帮助