Number Extension
isSuccessHttpResCode
Check if the numeric HTTP status code represents a successful response. Currently, 200 and 201 are considered successful.
final isSuccess = 200.isSuccessHttpResCode; // true
final isSuccess = 200.isSuccessHttpResCode; // true
toHttpResStatus
Converts the numeric HTTP status code to its corresponding HttpResStatus
enum.
final status = 404.toHttpResStatus; // HttpResStatus.notFound
final status = 404.toHttpResStatus; // HttpResStatus.notFound
tryToInt
Safely converts a nullable number to an integer.
final integer = someNum?.tryToInt();
final integer = someNum?.tryToInt();
tryToDouble
Safely converts a nullable number to a double.
final doubleValue = someNum?.tryToDouble();
final doubleValue = someNum?.tryToDouble();
percentage
Calculates the percentage this number represents out of a total without decimal numbers.
final total = 1000;
final perc = 250?.percentage(total); // 23%
final total = 1000;
final perc = 250?.percentage(total); // 23%
isNegative
& isPositive
Check if the number is negative or positive.
final bool neg = someNum?.isNegative;
final bool pos = someNum?.isPositive;
final bool neg = someNum?.isNegative;
final bool pos = someNum?.isPositive;
isZeroOrNull
Check if the number is zero or null.
final zeroOrNull = someNum?.isZeroOrNull; // true or false
final zeroOrNull = someNum?.isZeroOrNull; // true or false
asBool
Evaluates the truthiness of a nullable number based on whether it's greater than 0.
final bool truthy = someNum?.asBool; // true or false
final bool truthy = someNum?.asBool; // true or false
isZero
Check if the number is zero.
final bool zero = someNum.isZero; // true or false
final bool zero = someNum.isZero; // true or false
numberOfDigits
Returns the number of digits in the number.
final digits = someNum.numberOfDigits; // Returns count of digits
final digits = someNum.numberOfDigits; // Returns count of digits
removeTrailingZero
Removes trailing zeros from the number when converted to a string.
final str = someNum.removeTrailingZero; // "12" for 12.00, "12.34" for 12.34
final str = someNum.removeTrailingZero; // "12" for 12.00, "12.34" for 12.34
roundToFiftyOrHundred
Rounds the number up to the nearest fifty or hundred.
final rounded = someNum.roundToFiftyOrHundred; // 150 for 123, 200 for 190
final rounded = someNum.roundToFiftyOrHundred; // 150 for 123, 200 for 190
roundToTenth
Rounds the number up to the nearest tenth.
final rounded = someNum.roundToTenth; // 20 for 18, 30 for 26
final rounded = someNum.roundToTenth; // 20 for 18, 30 for 26
tenth
Returns one-tenth of the number.
final part = someNum.tenth; // 2 for 20, 1.2 for 12
final part = someNum.tenth; // 2 for 20, 1.2 for 12
fourth
Returns one-fourth of the number.
final part = someNum.fourth; // 5 for 20, 3 for 12
final part = someNum.fourth; // 5 for 20, 3 for 12
third
Returns one-third of the number.
final part = someNum.third; // 6.666... for 20, 4 for 12
final part = someNum.third; // 6.666... for 20, 4 for 12
half
Returns half of the number.
final part = someNum.half; // 10 for 20, 6 for 12
final part = someNum.half; // 10 for 20, 6 for 12
asGreeks
Converts the number into a more human-readable format using Greek symbols for large numbers.
final anyNumberType = 1500;
final greek = anyNumberType.asGreeks; // '1.5K'
// 1500000 -> "1.5M", 2500000000 -> "2.5B", etc.
final anyNumberType = 1500;
final greek = anyNumberType.asGreeks; // '1.5K'
// 1500000 -> "1.5M", 2500000000 -> "2.5B", etc.
delay
Utility to delay code execution or callback for a number of seconds.
await someNum.delay(); // Delays for 'someNum' seconds
await someNum.delay(); // Delays for 'someNum' seconds
daysDelay
Utility to delay code execution or callback for a number of days.
await someNum.daysDelay; // Delays for 'someNum' days
await someNum.daysDelay; // Delays for 'someNum' days
hoursDelay
Utility to delay code execution or callback for a number of hours.
await someNum.hoursDelay; // Delays for 'someNum' hours
await someNum.hoursDelay; // Delays for 'someNum' hours
minDelay
Utility to delay code execution or callback for a number of minutes.
await someNum.minDelay; // Delays for 'someNum' minutes
await someNum.minDelay; // Delays for 'someNum' minutes
secDelay
Utility to delay code execution or callback for a number of seconds.
await someNum.secDelay; // Delays for 'someNum' seconds
await someNum.secDelay; // Delays for 'someNum' seconds
millisecondsDelay
Utility to delay code execution or callback for a number of milliseconds.
await someNum.millisecondsDelay; // Delays for 'someNum' milliseconds
await someNum.millisecondsDelay; // Delays for 'someNum' milliseconds
asMilliseconds
, asSeconds
, asMinutes
, asHours
, asDays
Utilities to convert the number into a Duration object for various time units.
final ms = someNum.asMilliseconds; // Converts to Duration in milliseconds
final sec = someNum.asSeconds; // Converts to Duration in seconds
final min = someNum.asMinutes; // Converts to Duration in minutes
final hr = someNum.asHours; // Converts to Duration in hours
final days = someNum.asDays; // Converts to Duration in days
final ms = someNum.asMilliseconds; // Converts to Duration in milliseconds
final sec = someNum.asSeconds; // Converts to Duration in seconds
final min = someNum.asMinutes; // Converts to Duration in minutes
final hr = someNum.asHours; // Converts to Duration in hours
final days = someNum.asDays; // Converts to Duration in days
until
Utility to generate a sequence of numbers from this number to the specified end number.
final sequence = someNum.until(10); // [someNum, someNum+1, ..., 9]
final sequence = someNum.until(10); // [someNum, someNum+1, ..., 9]
inRangeOf(min, max)
Clamps the number within the given range.
min
: Minimum allowed value.max
: Maximum allowed value.
Throws Exception if either min
or max
is null. Throws ArgumentError if min
is greater than max
.
var num = 5;
print(num.inRangeOf(1, 10)); // Output: 5
var num = 5;
print(num.inRangeOf(1, 10)); // Output: 5
absolute
Returns the absolute value of the number.
var num = -5;
print(num.absolute); // Output: 5
var num = -5;
print(num.absolute); // Output: 5
doubled
Doubles the value of the number.
var num = 5;
print(num.doubled); // Output: 10
var num = 5;
print(num.doubled); // Output: 10
tripled
Triples the value of the number.
var num = 5;
print(num.tripled); // Output: 15
var num = 5;
print(num.tripled); // Output: 15
quadrupled
Quadruples the value of the number.
var num = 5;
print(num.quadrupled); // Output: 20
var num = 5;
print(num.quadrupled); // Output: 20
squared
Squares the value of the number.
var num = 5;
print(num.squared); // Output: 25
var num = 5;
print(num.squared); // Output: 25