[Unity动态改变数字与科学计数显示数值]

 ///


    /// 动态改变数字
    ///

    /// 原始值
    /// 目标值
    /// 改变次数
    /// 改变数值的赋值文本
    /// 改变类型  上升,下降
    /// 每次改变的时间
    ///
    IEnumerator ChangeAdd(float oriNum, float tarNum, float chang_speed, TMP_Text changeText,ChangeType changeType,float ChangeTime)
    {
        float result = oriNum;
        float delta = (Math.Abs(tarNum - oriNum)) / chang_speed;//每次变化的数的大小
        Math.Round(delta, 2);//只保留2位
        for (int i = 0; i < chang_speed; i++)
        {
            if(changeType== ChangeType.Add)
            {
            result += delta;
            }
            else
            {
                result -= delta;
            }
            //changeText.text = result.ToString();
            changeText.text = ScienceCount.KXJSF(result);//科学计数   例如0.1=1E-1   10=1E+1
            yield return new WaitForSeconds(ChangeTime);
        }
        //changeText.text = max.ToString();
        changeText.text = ScienceCount.KXJSF(tarNum);
        StopCoroutine(ChangeAdd(oriNum, tarNum, chang_speed, changeText,changeType,ChangeTime));
    }
    enum ChangeType
    {
        Add,
        Sub
    }

科学计数法

需求:输入一个数,用科学计数法表示,要有三位有效数字,幂数部分也要求是三位,不足则补零

代码如下:

public abstract class ScienceCount
    {
        public static string KXJSF(double num)
        {
            double bef =System.Math.Abs(num);
            int aft=0;
            while (bef >= 10 ||(bef < 1&& bef!=0))
            {
                if (bef >= 10)
                {
                    bef = bef / 10;
                    aft++;
                }
                else
                {
                    bef = bef * 10;
                    aft--;
                }
            }
            return string.Concat(num>=0?"":"-",ReturnBef(bef),"E",ReturnAft(aft));
        }
        ///


        /// 有效数字的处理
        ///

        /// 有效数字
        /// 三位有效数字,不足则补零
        public static string ReturnBef(double bef)
        {
            if (bef.ToString() != null)
            {
                char[] arr = bef.ToString().ToCharArray();
                switch (arr.Length)
                {
                    case 1:
                    case 2: return string.Concat(arr[0] , "." , "00"); break;
                    case 3: return string.Concat(arr[0] + "." + arr[2] + "0"); break;
                    default: return string.Concat(arr[0] + "." + arr[2] + arr[3]); break;
                }
            }
            else
                return "000";
        }
        ///
        /// 幂的处理
        ///

        /// 幂数
        /// 三位幂数部分,不足则补零
        public static string ReturnAft(int aft)
        {
            if (aft.ToString() != null)
            {
                string end;
                char[] arr = System.Math.Abs(aft).ToString().ToCharArray();
                switch (arr.Length)
                {
                    case 1: end = "00" + arr[0]; break;
                    case 2: end = "0" + arr[0] + arr[1]; break;
                    default: end = System.Math.Abs(aft).ToString(); break;
                }
                return string.Concat(aft >= 0 ? "+" : "-" , end);
            }
            else
                return "+000";
        }
    }

你可能感兴趣的:(c#,unity)