コード例 #1
0
        public static string Scientific(double number, int sigFigs = 2)
        {
            var    decimalPoint = (int)System.Math.Floor(System.Math.Log10(System.Math.Abs(number)));
            double multiplier   = FuncFuncs.IntPower(10, decimalPoint);
            double centered     = number / multiplier;

            if (sigFigs < 1)
            {
                sigFigs = 1;
            }
            if (decimalPoint == 0)
            {
                return(System.Math.Round(centered, sigFigs - 1).ToString());
            }
            return(System.Math.Round(centered, sigFigs - 1) + "e" + decimalPoint);
        }
コード例 #2
0
        public static double RoundToSigFigs(double number, int sigFigs = 2)
        {
            //range -> decimalPoint -> multiplier -> centered
            //10-99.99.. -> 1 -> 10 -> 1-9.99..
            //1-9.99... -> 0 -> 1 -> 1-9.99..
            //0.1-0.99... -> -1 -> 0.1 -> 1-9.99..
            var    decimalPoint = (int)System.Math.Floor(System.Math.Log10(System.Math.Abs(number)));
            double multiplier   = FuncFuncs.IntPower(10, decimalPoint);
            double centered     = number / multiplier;

            if (sigFigs < 1)
            {
                sigFigs = 1;
            }
            return(System.Math.Round(centered, sigFigs - 1) * multiplier);
        }
コード例 #3
0
        public static string NiceNumber(double number, int sigFigs = 2, int maxZeros = 2, bool space = false)
        {
            if (number == 0)
            {
                return(0 + (space ? " " : ""));
            }
            var    decimalPoint = (int)System.Math.Floor(System.Math.Log10(System.Math.Abs(number)));
            double multiplier   = FuncFuncs.IntPower(10, decimalPoint);
            double centered     = number / multiplier;

            if (sigFigs < 1)
            {
                sigFigs = 1;
            }

            if (decimalPoint > sigFigs + maxZeros - 1 ||
                decimalPoint < -1 - maxZeros)
            {
                return(System.Math.Round(centered, sigFigs - 1) + "e" + decimalPoint + (space ? " " : ""));
            }
            return((System.Math.Round(centered, sigFigs - 1) * multiplier).ToString(NumberFormat) + (space ? " " : ""));
        }