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

http://msdn.microsoft.com/en-us/library/aa325113%28VS.71%29.aspx

But you could do something like this...

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

nope, that does not work
the output should be 1:15 and the output I get with your code is
AM 12:00

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 :P

This is what I have sofar

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.

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

        }
    }
commented: He never divides by zero! +15

Dan the mathematician :)

commented: LOL, to much of an honour, but thanks anyway! +14

thank you very much
Worked like a charm

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.