943,929 Members | Top Members by Rank

Ad:
  • C# Discussion Thread
  • Marked Solved
  • Views: 6556
  • C# RSS
Sep 22nd, 2009
0

format decimal value to time

Expand Post »
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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
procomp65 is offline Offline
48 posts
since Aug 2009
Sep 22nd, 2009
0

Re: format decimal value to time

http://msdn.microsoft.com/en-us/libr...8VS.71%29.aspx

But you could do something like this...
c# Syntax (Toggle Plain Text)
  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.
Reputation Points: 55
Solved Threads: 18
Junior Poster
papanyquiL is offline Offline
168 posts
since May 2009
Sep 22nd, 2009
0

Re: format decimal value to time

nope, that does not work
the output should be 1:15 and the output I get with your code is
AM 12:00
Reputation Points: 10
Solved Threads: 0
Light Poster
procomp65 is offline Offline
48 posts
since Aug 2009
Sep 22nd, 2009
0

Re: format decimal value to time

Quote ...
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
Reputation Points: 55
Solved Threads: 18
Junior Poster
papanyquiL is offline Offline
168 posts
since May 2009
Sep 22nd, 2009
0

Re: format decimal value to time

This is what I have sofar

C# Syntax (Toggle Plain Text)
  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. }
Reputation Points: 10
Solved Threads: 0
Light Poster
procomp65 is offline Offline
48 posts
since Aug 2009
Sep 22nd, 2009
1

Re: format decimal value to time

Perhaps you can try this, it works with doubles but you can convert decimal to double.

c# Syntax (Toggle Plain Text)
  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. }
Reputation Points: 2035
Solved Threads: 645
Senior Poster
ddanbe is offline Offline
3,740 posts
since Oct 2008
Sep 22nd, 2009
1

Re: format decimal value to time

Dan the mathematician
Reputation Points: 55
Solved Threads: 18
Junior Poster
papanyquiL is offline Offline
168 posts
since May 2009
Sep 24th, 2009
0

Re: format decimal value to time

thank you very much
Worked like a charm
Reputation Points: 10
Solved Threads: 0
Light Poster
procomp65 is offline Offline
48 posts
since Aug 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C# Forum Timeline: CDOSYS with C#
Next Thread in C# Forum Timeline: Problem when opening Photoshop in C#





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC