这是尝试将拆分字符串转换为int格式以显示用户输入时间,例如2:15,表示2小时15分钟。我希望能够将此时间段乘以美元汇率并增加额外费用。有一个subAmount,然后是一个使用ToString的总数。任何'线索'将不胜感激
private void calulateButton_Click(object sender, EventArgs e)
{
string[] item = timeTextBox.Text.Split(':');
int hours, mins, rate, cost;
int.TryParse(item[0], out hours);
Convert.ToInt64(item[0]);
int.TryParse(item[1], out hours);
Convert.ToInt64(item[1]);
int.TryParse(rateTextBox.Text, out rate);
int.TryParse(prescriptionTextBox.Text, out cost);
int prescription = cost;
int totalHours = hours;
subAmountLabel.Text = totalHours.ToString("f2");
int total = (totalHours) * rate + cost;
totalAmountLabel.Text = total.ToString("f2");
}
答案 0 :(得分:0)
您的代码示例似乎完全有效(除了您分配hours
两次)但它包含一些冗余行。我修复了hours
和mins
的问题,删除了不必要的变量和说明,并编辑了total
公式以包含会议记录:
private void calulateButton_Click(object sender, EventArgs e)
{
string[] item = timeTextBox.Text.Split(':');
int hours, mins;
int.TryParse(item[0], out hours);
int.TryParse(item[1], out mins);
double rate, prescription;
double totalHours = hours + mins / 60.0;
double.TryParse(rateTextBox.Text, out rate);
double.TryParse(prescriptionTextBox.Text, out prescription);
double total = totalHours * rate + prescription;
subAmountLabel.Text = totalHours.ToString("G");
totalAmountLabel.Text = total.ToString("C");
}
答案 1 :(得分:0)
在我认为你想要做的事情中,这只是一个黑暗的镜头:
TimeSpan ts = TimeSpan.Parse(timeTextBox.Text);
double rate, cost;
double.TryParse(rateTextBox.Text, out rate);
double.TryParse(prescriptionTextBox.Text, out cost);
subAmountLabel.Text = ts.TotalHours.ToString("f2");
double total = ts.TotalHours * rate + cost;
totalAmountLabel.Text = total.ToString("f2");