I want to convert minutes to seconds and at the moment I have a problem because in the minute textbox when I type 1.50 the outcome is 90 seconds which is wrong because 1.30 = 90 seconds
private void MtoCbutton_Click(object sender, EventArgs e)
{
if (minTosecTextBox.Text != "Minutes")
{
minutes = Convert.ToDouble(minTosecTextBox.Text);
TimeSpan span = TimeSpan.FromMinutes(minutes);
resultSectextBoxtextBox.Text = span.TotalSeconds.ToString();
}
else
{
MessageBox.Show("Please enter Minutes");
}
To convert from seconds to minutes you simply need to divide by 60.0 (you need the decimal or it will be treated like an integer). If treated like an integer and you pass 30 seconds, 30/60 will equal 0.
Also use double.TryParse
method. Right now if someone enters 1.50xx, your application will crash. Either use double.TryParse
method or use a try catch mechanism or only allow numeric entry.
EDIT
This will accomplish what you want. I added a label to show the output but you can remove it.
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");
}
EDIT 2
Some further clarification
You are not converting minutes to seconds since if you were then 1.5 (1 minute and a half) would equal 90 seconds. This is logical and obvious. You are treating only the part before the decimal as minutes and the part after the decimal is to be treated as seconds (1.30 = 1 minute and 30 seconds = 90 seconds).
Therefore we only need to convert the part before the decimal to seconds and add to it the part after the decimal.
You are probably looking for something like the TimeSpan.Parse method:
var ts = TimeSpan.Parse("00:01:30");
This will produce a TimeSpan of 90 seconds.
There is also a ParseExact
method, which lets you specify a format string, so you don't have to specify the hours each time, and lets you even specify a dot as a separator:
var ts = TimeSpan.ParseExact("01.30", @"mm\.ss", System.Globalization.CultureInfo.InvariantCulture);
For the format string, see Custom Date and Time format strings.
Also note that you need to escape every character that is to be interpreted literally (hence the \
before the .
).
Full code for your case:
var ts = TimeSpan.ParseExact(minTosecTextBox.Text, @"mm\.ss", System.Globalization.CultureInfo.InvariantCulture);
resultSectextBoxtextBox.Text = ts.TotalSeconds.ToString();
You need to handle the conversion of the text differently. You are converting "1.50" directly to a double, which is 1.5 and 1 and 1/2 minutes would be equivalent to 90 seconds. If you want to allow the user to type "1.50" and mean, 1 minute and 50 seconds then you will need to split the string. You could do something like this:
var split = text.Split('.');
int seconds = 0;
if (split.Length > 1)
{
seconds = int.Parse(split[1]);
}
seconds += TimeSpan.FromMinutes(split[0]).Seconds;
Note - if you don't validate the text, than you could end up with this failing. You should probably use a TryParse to validate the seconds. Parse can throw an exception.