float类型数值逐渐刷新至指定的目标值

	/// <summary>
    /// 指定数值逐渐刷新至目标数值
    /// </summary>
    /// <param name="startValue">起始数值</param>
    /// <param name="targetValue">目标数值</param>
    /// <param name="timer">数值刷新间隔时间</param>
    /// <param name="frequency">数值刷新次数</param>
    /// <returns></returns>
    IEnumerator RefreshNum(float startValue, float targetValue, float timer, int frequency)
    {
        //差值
        float differenceValue = Mathf.Abs(startValue - targetValue);
        if ((startValue - targetValue) > 0)//起始数值大于目标数值    递减
        {
            while (startValue > targetValue)
            {
                yield return new WaitForSeconds(timer);
                startValue -= (differenceValue) / frequency;
                if (startValue < targetValue)
                {
                    startValue = targetValue;
                }
                Debug.Log("递减:  "+System.String.Format("{0:F1}", startValue));
            }
        }//起始数值大小于目标数值    递増
        else
        {
            while (startValue < targetValue)
            {
                yield return new WaitForSeconds(timer);
                startValue += (differenceValue) / frequency;
                if (startValue > targetValue)
                {
                    startValue = targetValue;
                }
                Debug.Log("递增:  " + System.String.Format("{0:F1}", startValue));
            }
        }
    }

PS:读取时刷新数值时,碰巧刷新的数值时整数时,有的方法保留一位小数不好用,下面是整理内容

//eg:显示保留一位小数

//1,当值碰巧为整数时,不显示小数点
float.ToString("0.#");

//2,当值碰巧为整数时,不显示小数点
Math.Round(float, 1);

//2,当值碰巧为整数时,依然显示小数点后一位
String.Format("{0:F1}", float);
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值