LitDev.LDMath.FixSigFig C# (CSharp) Method

FixSigFig() public static method

Round a number to a fixed number of significant figures.
public static FixSigFig ( Primitive number, Primitive digits ) : Primitive
number Primitive The number to change.
digits Primitive The number of significant figures.
return Primitive
        public static Primitive FixSigFig(Primitive number, Primitive digits)
        {
            if (number == 0 || number+0 != number) return number;

            double scale = System.Math.Pow(10, System.Math.Floor(System.Math.Log10(System.Math.Abs((double)number))) + 1);
            return scale * System.Math.Round((double)number / scale, digits);
        }