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
-
- num?
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 betweenlowerLimitandupperLimit. -
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. Setinclusiveto 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 giventotal. Returns 0.0 iftotalis 0 or null. -
roundToPrecision(
int precision) → double -
Available on num?, provided by the NumExtensions extension
Rounds the validated number to a specified number of decimalprecision. 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 offractionDigits. Returns "0.00" (or based onfractionDigits) 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.