| | |
format decimal value to time
Please support our C# advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: May 2009
Posts: 114
Reputation:
Solved Threads: 11
http://msdn.microsoft.com/en-us/libr...8VS.71%29.aspx
But you could do something like this...
And make a method to format the string so it displays just the time
But you could do something like this...
c# Syntax (Toggle Plain Text)
double value = 1.25; string str = Convert.ToString(value); DateTime dt = Convert.ToDateTime(str); 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.
•
•
Join Date: Aug 2009
Posts: 40
Reputation:
Solved Threads: 0
This is what I have sofar
C# Syntax (Toggle Plain Text)
public static string FractionalMinutesToString(decimal minutes, string format) { if (string.IsNullOrEmpty(format)) format = "{0}:{1}"; TimeSpan tspan = TimeSpan.FromMinutes((double)minutes); return string.Format(format, tspan.Minutes, tspan.Seconds); } public static string FractionalHoursToString(decimal minutes) { return FractionalMinutesToString(minutes, null); } private void toolStripSplitButton1_Click(object sender, EventArgs e) { Double transitTime = Convert.ToDouble(subData1[13]); double newTransitTime = (transitTime * 0.1); transitTime = FractionalHoursToString(newTransitTime, ); txtTransmitTime.Text = transitTime.ToString(); }
Perhaps you can try this, it works with doubles but you can convert decimal to double.
c# Syntax (Toggle Plain Text)
class Program { static void Main(string[] args) { double DecNum = 13.573451; string TheTime = MakeTimeString(DecNum); Console.WriteLine(TheTime); DecNum = 1.25; TheTime = MakeTimeString(DecNum); Console.WriteLine(TheTime); Console.ReadKey(); // hold console until a key is pressed } static string MakeTimeString(double num) { double hours = Math.Floor(num); //take integral part double minutes = (num - hours) * 60.0; //multiply fractional part with 60 //double seconds = (minutes - Math.Floor(minutes)) * 60.0; //add if you want seconds int H = (int)Math.Floor(hours); int M = (int)Math.Floor(minutes); //int S = (int)Math.Floor(seconds); //add if you want seconds return H.ToString() + ":" + M.ToString(); //+":" + S.ToString(); //add if you want seconds } }
Today is a gift, that's why it is called "The Present".
Make love, no war. Cave ab homine unius libri.
Danny
Make love, no war. Cave ab homine unius libri.
Danny
![]() |
Similar Threads
- Format to 2 decimal places (VB.NET)
- Format Decimal (C#)
- Changing Time format (Pascal and Delphi)
- New Time Format (DaniWeb Community Feedback)
Other Threads in the C# Forum
- Previous Thread: CDOSYS with C#
- Next Thread: Problem when opening Photoshop in C#
Views: 1154 | Replies: 7
| Thread Tools | Search this Thread |
Tag cloud for conversion, math, time
abrupt alarm algorithm angle array base binary bitmap c# c++ class click clock clocks compile conversion converter count cuil data date datetime decimal degrees design designer development digit division drawing enum equation execution feet form format function gcd gdi+ geek google grade inches integer iteration java javascript lapse linux mandelbrot math mathematics method millimeter news number numbers numbertoword operator packing picturebox plotting polynomial prime primenumbersinrange program python radians radix recursion recursive reference refresh round running save schedule search sort statistics studio technology temperature time timespan tlapse unicode unit unix unsignedlong usb vb vbnet vista visual web wolframalpha word







