public static class FloatExtension
{
/// <summary>
/// 将秒数转化为00:00:00格式
/// </summary>
/// <param name="time">秒数</param>
/// <returns>00:00:00</returns>
public static string ToTimeFormat(this float time)
{
//秒数取整
int seconds = (int)time;
//一小时为3600秒 秒数对3600取整即为小时
int hour = seconds / 3600;
//一分钟为60秒 秒数对3600取余再对60取整即为分钟
int minute = seconds % 3600 / 60;
//对3600取余再对60取余即为秒数
seconds = seconds % 3600 % 60;
//返回00:00:00时间格式
return string.Format("{0:D2}:{1:D2}:{2:D2}", hour, minute, seconds);
}
}
public class Foo : MonoBehaviour
{
private void Start()
{
Debug.Log(300.38f.ToTimeFormat());
Debug.Log(456.14f.ToTimeFormat());
Debug.Log(48213.7f.ToTimeFormat());
}
}
欢迎关注公众号 “当代野生程序猿”