NumExtensions extension

Extensions on nullable num for validation, range checks, formatting, and mathematical helpers.

These work on both int and double since they extend num. Every method is null-safe — if the value is null, validate kicks in and treats it as 0.

num? value;
value.isPositive;            // false (null → 0 → not positive)
3.14.roundToPrecision(1);    // 3.1
75.percentageOf(200);        // 37.5
on

Properties

isInteger → bool

Available on num?, provided by the NumExtensions extension

Returns true if the validated number has no fractional part.
no setter
isNegative → bool

Available on num?, provided by the NumExtensions extension

Returns true if the validated number is less than 0.
no setter
isPositive → bool

Available on num?, provided by the NumExtensions extension

Returns true if the validated number is greater than 0.
no setter
isZero → bool

Available on num?, provided by the NumExtensions extension

Returns true if the validated number is equal to 0.
no setter

Methods

abs() → num

Available on num?, provided by the NumExtensions extension

Returns the absolute value of the validated number.
clamp(num lowerLimit, num upperLimit) → num

Available on num?, provided by the NumExtensions extension

Clamps the validated number to between lowerLimit and upperLimit.
isBetween(num min, num max, {bool inclusive = true}) → bool

Available on num?, provided by the NumExtensions extension

Checks if the validated number falls within a specified range [min, max]. By default, the check is inclusive. Set inclusive to false for an exclusive check.
percentageOf(num total) → double

Available on num?, provided by the NumExtensions extension

Calculates what percentage the validated number is of a given total. Returns 0.0 if total is 0 or null.
roundToPrecision(int precision) → double

Available on num?, provided by the NumExtensions extension

Rounds the validated number to a specified number of decimal precision. For example, (10.12345).roundToPrecision(2) will return 10.12
toDoubleDigits(int fractionDigits) → String

Available on num?, provided by the NumExtensions extension

Formats the validated number to a string with a fixed number of fractionDigits. Returns "0.00" (or based on fractionDigits) if the number is null.
validate({num value = 0}) → num

Available on num?, provided by the NumExtensions extension

Validate given double is not null and returns given value if null.