3DTD/Assets/Scripts/Extensions/NumberExtensions.cs

27 lines
1.1 KiB
C#

using UnityEngine;
#if ENABLED_UNITY_MATHEMATICS
using Unity.Mathematics;
#endif
namespace UnityUtils {
public static class NumberExtensions {
public static float PercentageOf(this int part, int whole) {
if (whole == 0) return 0; // Handling division by zero
return (float) part / whole;
}
public static int AtLeast(this int value, int min) => Mathf.Max(value, min);
public static int AtMost(this int value, int max) => Mathf.Min(value, max);
#if ENABLED_UNITY_MATHEMATICS
public static half AtLeast(this half value, half max) => MathfExtension.Max(value, max);
public static half AtMost(this half value, half max) => MathfExtension.Min(value, max);
#endif
public static float AtLeast(this float value, float min) => Mathf.Max(value, min);
public static float AtMost(this float value, float max) => Mathf.Min(value, max);
public static double AtLeast(this double value, double min) => MathfExtension.Max(value, min);
public static double AtMost(this double value, double min) => MathfExtension.Min(value, min);
}
}