C# 格式化时间

前言

格式化一个倒计时,GitHub Copilot自动帮我写完了。

代码

    void Update()
    {
        timer += Time.deltaTime;
        float gameSec = Mathf.Floor(timer);
        //格式化秒为00:00
       	float time = Mathf.Floor(gameSec);
		float min = Mathf.Floor(time / 60);
		float sec = time % 60;
		string minStr = min < 10 ? "0" + min : min.ToString();
		string secStr = sec < 10 ? "0" + sec : sec.ToString();
		Debug.Log(minStr+":"+ secStr);
    }

效果

效果图

Logo

汇聚全球AI编程工具,助力开发者即刻编程。

更多推荐