C# NumberHelper

Date: 2022-07-14
public static class NumberHelper
{
    public static bool IsAlmostZero(double d) => Math.Abs(d) < 0.0000001;
    public static bool IsEqualTo(double a, double b, double maxDiff = double.Epsilon) => Math.Abs(b - a) <= maxDiff;
    public static double SafeCalculatePercentage(double share, double whole) => IsEqualTo(whole, 0.0) ? 100 : share / whole * 100;
    public static int DoubleToIntCeiling(double d) => (int)Math.Ceiling(d);
    public static double GetPercentageDifference(double expected, double current) => Math.Abs((current - expected) / expected * 100);
    public static double GetPercentage(double whole, double part) => Math.Abs(part / whole * 100);
    public static bool IsNumeric(object o) => o is byte || o is sbyte || o is ushort || o is uint || o is ulong || o is short || o is int || o is long || o is float || o is double || o is decimal;
    public static double Limit(double val, double min, double max) => Math.Max(min, Math.Min(max, val));
    public static double Map(double val, double inMin, double inMax, double outMin, double outMax) 
	    => (Limit(val, inMin, inMax) - inMin) * (outMax - outMin) / (inMax - inMin) + outMin;
	public static double RoundNumberUpBy(double number, double roundBy) => Math.Ceiling(number * 1.0 / roundBy) * roundBy; // Only add up
    public static double RoundNumberBy(double number, double roundBy) => Math.Round(number * 1.0 / roundBy) * roundBy;
}
63870cookie-checkC# NumberHelper