using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PushPlatform.Common.Util
{
    public class CalcUtil
    {
        /// <summary>
        /// 运算科学计数法
        /// </summary>
        /// <param name="basenum"></param>
        /// <param name="indexnum"></param>
        /// <returns></returns>
        public static string ToScientific(double basenum, double indexnum)
        {
            double power = Math.Pow(10, indexnum);

            double result = basenum * power;

            decimal a = (decimal)result;

            return a.ToString();
        }

        public static double GetScientificBaseNum(long number)
        {
            var str = number.ToString("E");

            var res = str.Substring(0, str.IndexOf("E"));

            return Convert.ToDouble(res);
        }

        public static int GetScientificIndexNum(long number)
        {
            var str = number.ToString("E");

            var res = str.Substring(str.IndexOf("E") + 1);

            return Convert.ToInt32(res);
        }

        public static string CalcScientifiWithSymbol(string basenum, string indexnum)
        {
            if (basenum.IsNullOrEmpty() || indexnum.IsNullOrEmpty())
                return "0";

            double num = 0;
            int index = 0;
            string symbol = "";

            if (basenum.StartsWith(">") || basenum.StartsWith("<"))
            {
                symbol = basenum.Substring(0, 1);
                basenum = basenum.Substring(1);
            }

            if (double.TryParse(basenum, out num) && int.TryParse(indexnum, out index))
                return symbol + ToScientific(num, index).ToString();

            return "0";
        }

        public static string GetScientifiBaseWithSymbol(string Scientifi)
        {
            string symbol = "";

            if (Scientifi.StartsWith(">") || Scientifi.StartsWith("<"))
            {
                symbol = Scientifi.Substring(0, 1);
                Scientifi = Scientifi.Substring(1);
            }

            return symbol + GetScientificBaseNum(Convert.ToInt64(Scientifi));
        }

        public static string GetScientifiIndexWithSymbol(string Scientifi)
        {
            if (Scientifi.StartsWith(">") || Scientifi.StartsWith("<"))
            {
                Scientifi = Scientifi.Substring(1);
            }

            return GetScientificIndexNum(Convert.ToInt64(Scientifi)).ToString();
        }
    }
}
