How to convert a float into time?

As an example lets say I have float total_Time = 228.10803 what I want is to convert total_Time into a time format so it would look like this 00:03:48 how would I do that?

I thought of writing it like this:

{

    float total_Time = 0;
    float h = 0;
    float m = 0;
    float s = 0;

    br.BaseStream.Position = i + 4;
    total_Time = br.ReadSingle(); //reads 228.10803
    total_Time = Convert.ToSingle(Math.Round(total_Time, 0));
    if (total_Time < 3600)
    {
        h = 0;
    }
    else
    {
        h = total_Time / 3600;
        h = Convert.ToSingle(Math.Round(h, 0));
        total_Time = total_Time - (h * 3600);
    }
    m = total_Time / 60;
    m = Convert.ToSingle(Math.Round(m, 0));
    total_Time = total_Time - (m * 60);
    s = total_Time;
    s = Convert.ToSingle(Math.Round(s, 0));
    SaveCS_PlayTime.Text = String.Format("Play Time: {0}:{1}:{2}", h, m, s);
}

But for some reason it shows it like this 00:4:-12. Maybe I have made a mistake. Really need some help with this. I know there are posts with practically same titles as mine but non had the answer I was looking for.

Jon Skeet
people
quotationmark

Sounds like you should convert it into a TimeSpan and then format that:

using System;

class Test
{
    static void Main()
    {
        float totalSeconds = 228.10803f;
        TimeSpan time = TimeSpan.FromSeconds(totalSeconds);
        Console.WriteLine(time.ToString("hh':'mm':'ss")); // 00:03:48
    }
}

An alternative would be to use my Noda Time library, where you'd construct a Duration and then format that.

people

See more on this question at Stackoverflow