年总和折旧 (SYD) 调试

Sum-of-the- years’ digits depreciation (SYD) debugging

我需要创建一个折旧计算器来计算直线折旧 (SLN) 和年数总和折旧 (SYD)。

SLN 和 SYD 函数应由程序员定义。

我能够获得 SLN(),但对于 SYD(),我只能获得最多 5 条生命的正确输出。如果超过这个数,它会显示错误的输出。

正确的输出

输出错误

这是我的代码

    private void buttonUpdate_Click(object sender, EventArgs e)
    {
        //validate input
        if (!isNumeric(textBoxICost.Text) || double.Parse(textBoxICost.Text ) < 0|| !isNumeric(textBoxSVal.Text) || double.Parse(textBoxSVal.Text) < 0)
            MessageBox.Show("Input amount must be numeric > 0");
        else if (double.Parse(textBoxICost.Text) <= double.Parse(textBoxSVal.Text))
            MessageBox.Show("Initial Cost must be greater than Salvage Cost");
        else
        {
            //declare and initialize
            double depreciation = 0, bookVal = 0;
            int life = int.Parse(comboBoxLife.GetItemText(comboBoxLife.SelectedItem));
            double asset = double.Parse(textBoxICost.Text);
            double sVal = double.Parse(textBoxSVal.Text);
            listBoxOutput.Items.Clear(); // clear ouput

            //check method
            if (radioButtonSLine.Checked)
            {
                depreciation = SLN(asset, sVal, life);
                bookVal = asset;
                listBoxOutput.Items.Add("YEAR\t\tDEPRECIATION\t\tBOOK VALUE");
                for (int x = 0; x < life; ++x)
                {
                    bookVal -= depreciation;

                    listBoxOutput.Items.Add(x + 1 + "\t\t$" + depreciation + "\t\t\t$" + bookVal);
                }
            }
            else
            {
                int y = life;
                bookVal = asset;
                listBoxOutput.Items.Add("YEAR\t\tDEPRECIATION\t\tBOOK VALUE");
                for (int x = 0; x < life; ++x)
                {
                    depreciation = (asset - sVal) * (y / SYD(life));
                    bookVal = bookVal - depreciation;
                    listBoxOutput.Items.Add(x + 1 + "\t\t$" + depreciation + "\t\t\t$" + bookVal);
                    --y;
                }

            }
        }

    }

    // programmer defined SYD function
    private double SYD(int life)
    {
        double depreciation;
        return depreciation = life * (life+1) / 2;
    }

有人可以检查我的代码吗,谢谢!

我发现账面价值因为精度问题没有显示。

我能够通过将小数位设置为 2 来修复它。

使用代码:string.Format("{0:F2}", value)