Number class

Static utility methods for working with numbers.

Number.format(1234567);              // '1,234,567'
Number.currency(1234.56);             // '$1,234.56'
Number.percentage(10);                // '10%'
Number.fileSize(1024);                // '1 KB'
Number.abbreviate(1500);              // '2K'
Number.forHumans(1500, maxPrecision: 1); // '1.5 thousand'
Number.ordinal(21);                   // '21st'
Number.spell(123);                    // 'one hundred twenty-three'

Available helpers:

Locale / currency defaults:

Formatting:

Ordinal / spelling (English):

Parsing:

Math / utility:

Random:

File-size inverse / duration:

Range / aggregates:

Properties

hashCode int
The hash code for this object.
no setterinherited
runtimeType Type
A representation of the runtime type of the object.
no setterinherited

Methods

noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
toString() String
A string representation of this object.
inherited

Operators

operator ==(Object other) bool
The equality operator.
inherited

Static Methods

abbreviate(num number, {int precision = 0, int? maxPrecision}) String
Returns an abbreviated form of number (1K, 1M, 1B).
average(Iterable<num> values) double
Returns the arithmetic mean of values, or 0 if empty.
between(num value, {required num min, required num max}) bool
Returns true when value is between min and max (inclusive).
ceil(num value, [int precision = 0]) double
Ceils value to precision decimal places.
clamp<T extends num>(T value, T min, T max) → T
Clamps value between min and max (inclusive).
currency(num number, {String? currency, String? locale}) String
Formats number as a currency value.
defaultCurrency() String
Returns the default currency code.
defaultLocale() String
Returns the default locale used by formatting methods.
degrees(num radians) double
Converts radians to degrees.
duration(num seconds, {bool short = true}) String
Formats seconds as a human-readable duration.
fileSize(num bytes, {int precision = 0, int? maxPrecision}) String
Formats bytes as a human-readable file size.
floor(num value, [int precision = 0]) double
Floors value to precision decimal places.
forHumans(num number, {int precision = 0, int? maxPrecision}) String
Returns a human-readable form of number (1 thousand, 1 million).
format(num number, {String? locale, int? precision, int? maxPrecision}) String
Formats number with locale-aware separators.
gcd(int a, int b) int
Greatest common divisor of a and b.
lcm(int a, int b) int
Least common multiple of a and b.
lerp(num a, num b, double t) double
Linearly interpolates between a and b by t (0..1).
max(Iterable<num> values) num
Returns the largest value in values.
median(Iterable<num> values) double
Returns the median of values, or 0 if empty.
min(Iterable<num> values) num
Returns the smallest value in values.
ordinal(int number) String
Returns the ordinal form of number (1st, 2nd, 3rd, 4th).
pairs(int to, int by, {int offset = 1}) List<List<int>>
Generates pairs (sub-ranges) from a range up to to, stepping by by.
parseFloat(String value, {String? locale}) double?
Parses value as a double, locale-aware. Returns null on failure.
parseInt(String value, {String? locale}) int?
Parses value as an int, locale-aware. Returns null on failure.
percentage(num number, {int precision = 0, int? maxPrecision, String? locale}) String
Formats number as a percentage. The number is treated as the whole percentage value: 10 produces '10%', not '1000%'.
radians(num degrees) double
Converts degrees to radians.
random({int min = 0, int max = 0x7fffffff, int? seed}) int
Returns a random integer in [min, max] (inclusive).
range(int start, int end, {int step = 1}) List<int>
Generates a list of integers from start (inclusive) to end (inclusive), stepping by step.
round(num value, [int precision = 0]) double
Rounds value to precision decimal places.
scale(num value, {required num fromMin, required num fromMax, required num toMin, required num toMax}) double
Re-maps value from one range to another.
spell(int number) String
Spells out number in English.
spellOrdinal(int number) String
Spells out the ordinal form of number in English (first, second, third).
sum(Iterable<num> values) num
Returns the sum of values.
toBytes(Object value, [String? unit]) num?
Converts a file size to bytes. Inverse of fileSize.
trim(num value) String
Removes trailing zeros from the decimal portion of value.
useCurrency(String currency) → void
Sets the default currency code.
useLocale(String locale) → void
Sets the default locale used by formatting methods.