要将秒转换为分钟,您只需除以 60.0(您需要小数,否则它将被视为整数)。如果将其视为整数并且经过 30 秒,则 30/60 将等于 0。
也用double.TryParse
方法。现在如果有人输入 1.50xx,你的应用程序就会崩溃。要么使用double.TryParse
方法,要么使用 try catch 机制,或者只允许输入数字。
编辑
这将完成你想要的。我添加了一个标签来显示输出,但您可以将其删除。
double enteredNumber;
if (double.TryParse(minTosecTextBox.Text, out enteredNumber))
{
// This line will get everything but the decimal so if entered 1.45, it will get 1
double minutes = Math.Floor(enteredNumber);
// This line will get the seconds portion from the entered number.
// If the number is 1.45, it will get .45 then multiply it by 100 to get 45 secs
var seconds = 100 * (enteredNumber - Math.Floor(enteredNumber));
// now we multiply minutes by 60 and add the seconds
var secondsTotal = (minutes * 60 + seconds);
this.labelSeconds.Text = secondsTotal.ToString();
}
else
{
MessageBox.Show("Please enter Minutes");
}
编辑 2
一些进一步的澄清
您没有将分钟转换为秒,因为如果您是 1.5(1 分半)将等于 90 秒。这是合乎逻辑且显而易见的。您仅将小数点前的部分视为分钟,小数点后的部分将视为秒(1.30 = 1 分钟和 30 秒 = 90 秒)。因此,我们只需要将小数点前的部分转换为秒,并加上小数点后的部分。