Gecko45444 - 23 days ago 7

C# Question

As an example lets say I have

`float total_Time = 228.10803`

`total_Time`

`00:03:48`

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`

Answer Source

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.