I am converting the fractional hour value in this format '00 .000 'to the hours and minutes format. So '02 .500 'will be '02: 30', and I also have to do it in the reverse order.
I built a test to see how this will work,
<%
TimeSpan tspan;
TimeSpan tspan2;
string TimeString;
string TimeString2;
string Converted;
double ConvTime;
for (int HH = 0; HH < 24; HH++)
{
for (int M1 = 0; M1 < 6; M1++)
{
for (int M2 = 0; M2 < 10; M2++)
{
TimeString = HH.ToString() + ":" + M1.ToString() + M2.ToString();
tspan = TimeSpan.Parse(TimeString);
ConvTime = Convert.ToDouble(tspan.TotalHours);
Converted = String.Format("{0:00.000}", ConvTime);
tspan2 = TimeSpan.FromHours(Convert.ToDouble(Converted));
TimeString2 = string.Format("{0:D1}:{1:D2}", tspan2.Hours, tspan2.Minutes);
Response.Write(TimeString + " = ");
Response.Write(Converted + " = ");
Response.Write(TimeString2 + "<br>");
}
}
}
%>
The problem I am facing is that when I convert to a string, I lose the remainder form in the conversion, which discards the value in the inverse conversion.
Here is the output for the first 10 seconds.
0:00 = 00.000 = 0:00
0:01 = 00.017 = 0:01
0:02 = 00.033 = 0:01
0:03 = 00.050 = 0:03
0:04 = 00.067 = 0:04
0:05 = 00.083 = 0:04
0:06 = 00.100 = 0:06
0:07 = 00.117 = 0:07
0:08 = 00.133 = 0:07
0:09 = 00.150 = 0:09
0:10 = 00.167 = 0:10
as you can see where I started, this is not always what I get.
How to correctly convert the time value string "HH: MM" to the fractional string "00.000" and go back correctly.
thanks
EDIT:
I tried this
TimeString = "02:02";
Converted = TimeSpan.Parse(TimeString).TotalHours.ToString("00.000");
tspan2 = TimeSpan.FromHours(Convert.ToDouble(Converted));
TimeString2 = string.Format("{0:D1}:{1:D2}", tspan2.Hours, tspan2.Minutes);
Converted = "02.033" TimeString2 = "2:01". "2:02". , , "00.000".