Skip to content

Float Extensions

Shmellyorc edited this page Aug 31, 2026 · 1 revision

FloatExtensions provides a comprehensive set of extension methods for float values, making common mathematical operations more intuitive and readable.


Overview

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

Clamping

Clamp

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);  // 0f

Saturate

Clamps the value between 0 and 1.

float value = 1.5f;
float saturated = value.Saturate();  // 1f

float value2 = -0.5f;
float saturated2 = value2.Saturate();  // 0f

Wrapping

Wrap (single parameter)

Wraps the value within the range [0, max).

float value = 5f;
float wrapped = value.Wrap(3f);  // 2f

float value2 = -1f;
float wrapped2 = value2.Wrap(3f);  // 2f

Wrap (min and max)

Wraps 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);  // 3f

Angle Conversion

ToRadians

Converts degrees to radians.

float radians = 90f.ToRadians();  // 1.5708 (PI/2)
float radians2 = 180f.ToRadians();  // 3.14159 (PI)

ToDegrees

Converts radians to degrees.

float degrees = MathHelper.PI.ToDegrees();  // 180f
float degrees2 = MathHelper.HalfPI.ToDegrees();  // 90f

Sign and Absolute

Sign

Gets the sign of the value (-1, 0, or 1).

float sign = 5f.Sign();   // 1
float sign2 = -3f.Sign(); // -1
float sign3 = 0f.Sign();  // 0

Abs

Gets the absolute value.

float abs = (-5f).Abs();  // 5f
float abs2 = 3.7f.Abs();  // 3.7f

Epsilon Equality

IsZero

Determines whether the value is approximately zero.

bool isZero = 0.0005f.IsZero();  // true (within epsilon)
bool isZero2 = 0.002f.IsZero();  // false

ApproxEquals

Determines whether the value is approximately equal to another value.

bool equal = 0.999f.ApproxEquals(1.000f);  // true
bool equal2 = 1.002f.ApproxEquals(1.000f);  // false

Rounding

RoundToInt

Rounds the value to the nearest integer.

int rounded = 3.7f.RoundToInt();  // 4
int rounded2 = 3.2f.RoundToInt();  // 3

FloorToInt

Floors the value to the nearest integer.

int floored = 3.7f.FloorToInt();  // 3
int floored2 = 3.2f.FloorToInt();  // 3

CeilToInt

Ceils the value to the nearest integer.

int ceiled = 3.7f.CeilToInt();  // 4
int ceiled2 = 3.2f.CeilToInt();  // 4

Round

Rounds the value to the specified number of decimal places.

float rounded = 3.14159f.Round(2);  // 3.14
float rounded2 = 3.14159f.Round(3);  // 3.142

Snap

Snaps the value to the nearest multiple of a grid size.

float snapped = 12.3f.Snap(5f);  // 10f
float snapped2 = 17.8f.Snap(5f);  // 20f

Formatting

ToPercent

Converts the value to a percentage string.

string percent = 0.75f.ToPercent();  // "75%"
string percent2 = 0.333f.ToPercent();  // "33%"

ToTimeString

Converts the value to a time string in M:SS format.

string time = 125f.ToTimeString();  // "2:05"
string time2 = 65f.ToTimeString();  // "1:05"

Conversion

ToBool

Converts the value to a boolean.

bool isTrue = 1f.ToBool();   // true
bool isTrue2 = -5f.ToBool(); // true
bool isFalse = 0f.ToBool();  // false

Interpolation

LerpTo

Linearly interpolates from this value to another.

float value = 0f.LerpTo(10f, 0.5f);  // 5f
float value2 = 10f.LerpTo(20f, 0.3f);  // 13f

Examples

Health Bar

public void UpdateHealthBar(float currentHealth, float maxHealth)
{
    float percentage = (currentHealth / maxHealth).Saturate();
    _healthBarWidth = percentage * _maxBarWidth;
}

Angle Normalization

public float NormalizeAngle(float angle)
{
    return angle.Wrap(0f, MathHelper.TwoPI);
}

public float NormalizeAngleDegrees(float angle)
{
    return angle.Wrap(0f, 360f);
}

Grid Snapping

public Vect2 SnapToGrid(Vect2 position, float gridSize)
{
    return new Vect2(
        position.X.Snap(gridSize),
        position.Y.Snap(gridSize)
    );
}

Display Time

public string GetTimeDisplay(float seconds)
{
    if (seconds < 0f)
        return "-" + Math.Abs(seconds).ToTimeString();
    return seconds.ToTimeString();
}

Input Smoothing

public float SmoothValue(float current, float target, float speed)
{
    return current.LerpTo(target, speed);
}

Summary

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

Back to Home

Clone this wiki locally