본문 바로가기
Unity3D/Unity & C#

Unity & C# ~ 시간을 나타내는 법(float to string)

by 캬캬백곰 2023. 11. 3.
728x90
float timeValue = 90; // second

try
{
    string time1 = string.Format("{0:#0}:{1:00}", Mathf.Floor(timeValue / 60), Mathf.Floor(timeValue) % 60);
    Debug.Log($"Change time second : {time1}");
}
catch(Exception e)
{
    Debug.LogError($"timeValue error {e}");
}

try
{
    string time2 = string.Format("{0:00}:{1:00}", Mathf.Floor(timeValue / 60), Mathf.Floor(timeValue) % 60);
    Debug.Log($"Change time second2 : {time2}");
}
catch (Exception e)
{
    Debug.LogError($"timeValue2 error {e}");
}

float timeValue2 = 4210; // second

try
{
    string hTime = string.Format("{0:00}:{1:00}:{2:00}", Mathf.Floor(timeValue2 / 3600), Mathf.Floor((timeValue2 %3600) / 60), Mathf.Floor((timeValue2 % 3600) % 60));
    Debug.Log($"Change time second3 : {hTime}");
}
catch (Exception e)
{
    Debug.LogError($"timeValue3 error {e}");
}



// 결과
// Change time second : 1:30
// Change time second2 : 01:30
// Change time second3 : 01:10:10
728x90
반응형