我正在做一个简单的直线折旧应用程序。我的列表框显示年份和折旧金额。在同一个列表框中,我想添加"总计"并总结所有折旧。我添加了应用程序应该如何显示的图片。顺便说一句,我们需要使用FOR循环。
private void calcButton_Click_1(object sender, EventArgs e)
{
//Declare Vairables
double cost, salvage, depreciation;
int usefulLife;
int total = 0;
//grab data
double.TryParse(assetCostTextBox.Text, out cost);
double.TryParse(salvageValueTextBox.Text, out salvage);
int.TryParse(usefulLifeNumericUpDown.Text, out usefulLife);
//Print heading
string formatCode = "{0,7}{1,20}";
depreciationScheduleListBox.Items.Add(string.Format(formatCode,
"Years:", "Depreciation:"));
depreciationScheduleListBox.Items.Add("");
//use for loop to calculate deprecation value of each year
int iterations = 0;
for (iterations = 1; iterations <= usefulLife; iterations += 1)
{
depreciation = (cost - salvage) * 1/usefulLife;
depreciationScheduleListBox.Items.Add(string.Format(formatCode,
iterations, depreciation.ToString("C2")));
}
答案 0 :(得分:0)
这个怎么样?
def fcopy(source, target):
with open(source, 'rb') as f:
data = f.read()
with open(target, 'wb') as t:
t.write(data)
fcopy("source.jpeg","dest.jpeg")
我将listBox的项目转换为IEnumerable。
然后,我得到double total = depreciationScheduleListBox.Items.Cast<string>().Select((string item) => Convert.ToDouble(System.Text.RegularExpressions.Regex.Match(item, "Depreciation: \\$(.+)").Groups[1].Value)).ToArray().Sum();
之后应该是数字的东西。
然后,我将此字符串输出转换为Double并获得最终Depreciation: $
然后获得其总和