format decimal value to time

Please support our C# advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Aug 2009
Posts: 40
Reputation: procomp65 is an unknown quantity at this point 
Solved Threads: 0
procomp65 procomp65 is offline Offline
Light Poster

format decimal value to time

 
0
  #1
Sep 22nd, 2009
Hi
I need to format a decimal value to time and then display it in a textbox in the proper format. eg:
double value = 1.25 to "1:15"

I do not have a clue how to go about it

Thanks
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 114
Reputation: papanyquiL is on a distinguished road 
Solved Threads: 11
papanyquiL papanyquiL is offline Offline
Junior Poster

Re: format decimal value to time

 
0
  #2
Sep 22nd, 2009
http://msdn.microsoft.com/en-us/libr...8VS.71%29.aspx

But you could do something like this...
  1. double value = 1.25;
  2. string str = Convert.ToString(value);
  3. DateTime dt = Convert.ToDateTime(str);
  4. textBox1.Text = dt.ToString();

And make a method to format the string so it displays just the time
Last edited by papanyquiL; Sep 22nd, 2009 at 4:41 pm.
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 40
Reputation: procomp65 is an unknown quantity at this point 
Solved Threads: 0
procomp65 procomp65 is offline Offline
Light Poster

Re: format decimal value to time

 
0
  #3
Sep 22nd, 2009
nope, that does not work
the output should be 1:15 and the output I get with your code is
AM 12:00
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 114
Reputation: papanyquiL is on a distinguished road 
Solved Threads: 11
papanyquiL papanyquiL is offline Offline
Junior Poster

Re: format decimal value to time

 
0
  #4
Sep 22nd, 2009
And make a method to format the string so it displays just the time
Although, there might be an easier approach... Wait for more replies
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 40
Reputation: procomp65 is an unknown quantity at this point 
Solved Threads: 0
procomp65 procomp65 is offline Offline
Light Poster

Re: format decimal value to time

 
0
  #5
Sep 22nd, 2009
This is what I have sofar

  1. public static string FractionalMinutesToString(decimal minutes, string format)
  2. {
  3. if (string.IsNullOrEmpty(format))
  4. format = "{0}:{1}"; TimeSpan tspan = TimeSpan.FromMinutes((double)minutes);
  5. return string.Format(format, tspan.Minutes, tspan.Seconds);
  6. }
  7. public static string FractionalHoursToString(decimal minutes)
  8. {
  9. return FractionalMinutesToString(minutes, null);
  10. }
  11. private void toolStripSplitButton1_Click(object sender, EventArgs e)
  12. {
  13. Double transitTime = Convert.ToDouble(subData1[13]);
  14. double newTransitTime = (transitTime * 0.1);
  15. transitTime = FractionalHoursToString(newTransitTime, );
  16. txtTransmitTime.Text = transitTime.ToString();
  17. }
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 2,048
Reputation: ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of 
Solved Threads: 309
ddanbe's Avatar
ddanbe ddanbe is offline Offline
Postaholic

Re: format decimal value to time

 
1
  #6
Sep 22nd, 2009
Perhaps you can try this, it works with doubles but you can convert decimal to double.

  1. class Program
  2. {
  3. static void Main(string[] args)
  4. {
  5. double DecNum = 13.573451;
  6. string TheTime = MakeTimeString(DecNum);
  7. Console.WriteLine(TheTime);
  8. DecNum = 1.25;
  9. TheTime = MakeTimeString(DecNum);
  10. Console.WriteLine(TheTime);
  11. Console.ReadKey(); // hold console until a key is pressed
  12. }
  13.  
  14. static string MakeTimeString(double num)
  15. {
  16. double hours = Math.Floor(num); //take integral part
  17. double minutes = (num - hours) * 60.0; //multiply fractional part with 60
  18. //double seconds = (minutes - Math.Floor(minutes)) * 60.0; //add if you want seconds
  19. int H = (int)Math.Floor(hours);
  20. int M = (int)Math.Floor(minutes);
  21. //int S = (int)Math.Floor(seconds); //add if you want seconds
  22. return H.ToString() + ":" + M.ToString(); //+":" + S.ToString(); //add if you want seconds
  23.  
  24. }
  25. }
Today is a gift, that's why it is called "The Present".
Make love, no war. Cave ab homine unius libri.
Danny
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 114
Reputation: papanyquiL is on a distinguished road 
Solved Threads: 11
papanyquiL papanyquiL is offline Offline
Junior Poster

Re: format decimal value to time

 
1
  #7
Sep 22nd, 2009
Dan the mathematician
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 40
Reputation: procomp65 is an unknown quantity at this point 
Solved Threads: 0
procomp65 procomp65 is offline Offline
Light Poster

Re: format decimal value to time

 
0
  #8
Sep 24th, 2009
thank you very much
Worked like a charm
Reply With Quote Quick reply to this message  
Reply

Tags
conversion, math, time

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C# Forum


Views: 1154 | Replies: 7
Thread Tools Search this Thread



Tag cloud for conversion, math, time
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC