-
Notifications
You must be signed in to change notification settings - Fork 1
Float Extensions
FloatExtensions provides a comprehensive set of extension methods for float values, making common mathematical operations more intuitive and readable.
| Feature | Description |
|---|---|
| Clamping | Clamp values between ranges or 0-1 |
| Wrapping | Wrap values within ranges |
| Angle Conversion | Convert between degrees and radians |
| Rounding | Round, floor, ceiling, and snap values |
| Sign and Absolute | Get sign or absolute value |
| Epsilon Equality | Compare values with epsilon tolerance |
| Formatting | Convert to percent or time strings |
Clamps the value between a minimum and maximum.
float value = 15f;
float clamped = value.Clamp(0f, 10f); // 10f
float value2 = -5f;
float clamped2 = value2.Clamp(0f, 10f); // 0fClamps the value between 0 and 1.
float value = 1.5f;
float saturated = value.Saturate(); // 1f
float value2 = -0.5f;
float saturated2 = value2.Saturate(); // 0fWraps the value within the range [0, max).
float value = 5f;
float wrapped = value.Wrap(3f); // 2f
float value2 = -1f;
float wrapped2 = value2.Wrap(3f); // 2fWraps the value within the range [min, max).
float value = 7f;
float wrapped = value.Wrap(0f, 5f); // 2f
float value2 = -2f;
float wrapped2 = value2.Wrap(0f, 5f); // 3fConverts degrees to radians.
float radians = 90f.ToRadians(); // 1.5708 (PI/2)
float radians2 = 180f.ToRadians(); // 3.14159 (PI)Converts radians to degrees.
float degrees = MathHelper.PI.ToDegrees(); // 180f
float degrees2 = MathHelper.HalfPI.ToDegrees(); // 90fGets the sign of the value (-1, 0, or 1).
float sign = 5f.Sign(); // 1
float sign2 = -3f.Sign(); // -1
float sign3 = 0f.Sign(); // 0Gets the absolute value.
float abs = (-5f).Abs(); // 5f
float abs2 = 3.7f.Abs(); // 3.7fDetermines whether the value is approximately zero.
bool isZero = 0.0005f.IsZero(); // true (within epsilon)
bool isZero2 = 0.002f.IsZero(); // falseDetermines whether the value is approximately equal to another value.
bool equal = 0.999f.ApproxEquals(1.000f); // true
bool equal2 = 1.002f.ApproxEquals(1.000f); // falseRounds the value to the nearest integer.
int rounded = 3.7f.RoundToInt(); // 4
int rounded2 = 3.2f.RoundToInt(); // 3Floors the value to the nearest integer.
int floored = 3.7f.FloorToInt(); // 3
int floored2 = 3.2f.FloorToInt(); // 3Ceils the value to the nearest integer.
int ceiled = 3.7f.CeilToInt(); // 4
int ceiled2 = 3.2f.CeilToInt(); // 4Rounds the value to the specified number of decimal places.
float rounded = 3.14159f.Round(2); // 3.14
float rounded2 = 3.14159f.Round(3); // 3.142Snaps the value to the nearest multiple of a grid size.
float snapped = 12.3f.Snap(5f); // 10f
float snapped2 = 17.8f.Snap(5f); // 20fConverts the value to a percentage string.
string percent = 0.75f.ToPercent(); // "75%"
string percent2 = 0.333f.ToPercent(); // "33%"Converts the value to a time string in M:SS format.
string time = 125f.ToTimeString(); // "2:05"
string time2 = 65f.ToTimeString(); // "1:05"Converts the value to a boolean.
bool isTrue = 1f.ToBool(); // true
bool isTrue2 = -5f.ToBool(); // true
bool isFalse = 0f.ToBool(); // falseLinearly interpolates from this value to another.
float value = 0f.LerpTo(10f, 0.5f); // 5f
float value2 = 10f.LerpTo(20f, 0.3f); // 13fpublic void UpdateHealthBar(float currentHealth, float maxHealth)
{
float percentage = (currentHealth / maxHealth).Saturate();
_healthBarWidth = percentage * _maxBarWidth;
}public float NormalizeAngle(float angle)
{
return angle.Wrap(0f, MathHelper.TwoPI);
}
public float NormalizeAngleDegrees(float angle)
{
return angle.Wrap(0f, 360f);
}public Vect2 SnapToGrid(Vect2 position, float gridSize)
{
return new Vect2(
position.X.Snap(gridSize),
position.Y.Snap(gridSize)
);
}public string GetTimeDisplay(float seconds)
{
if (seconds < 0f)
return "-" + Math.Abs(seconds).ToTimeString();
return seconds.ToTimeString();
}public float SmoothValue(float current, float target, float speed)
{
return current.LerpTo(target, speed);
}| Method | Description |
|---|---|
Clamp |
Clamps between min and max |
Saturate |
Clamps between 0 and 1 |
Wrap |
Wraps within a range |
ToRadians |
Converts degrees to radians |
ToDegrees |
Converts radians to degrees |
Sign |
Gets the sign of the value |
Abs |
Gets the absolute value |
IsZero |
Checks if approximately zero |
ApproxEquals |
Checks if approximately equal |
RoundToInt |
Rounds to nearest integer |
FloorToInt |
Floors to nearest integer |
CeilToInt |
Ceils to nearest integer |
Round |
Rounds to decimal places |
Snap |
Snaps to grid size |
ToPercent |
Converts to percentage string |
ToTimeString |
Converts to time string |
ToBool |
Converts to boolean |
LerpTo |
Interpolates to target |
- MathHelper
- FileHelper
- HashHelper
- JsonHelper
- MapHelper
- TextHelper
- SoundHelper
- AlignHelpers
- Instance Helper
- Enum Extensions
- String Extensions
- Int Extensions
- Float Extensions
- IEnumerable Extensions
- Random Extensions
- Sound Extensions
- Font Extensions