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, or0if empty. -
between(
num value, {required num min, required num max}) → bool -
Returns true when
valueis betweenminandmax(inclusive). -
ceil(
num value, [int precision = 0]) → double -
Ceils
valuetoprecisiondecimal places. -
clamp<
T extends num> (T value, T min, T max) → T -
Clamps
valuebetweenminandmax(inclusive). -
currency(
num number, {String? currency, String? locale}) → String -
Formats
numberas 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
secondsas a human-readable duration. -
fileSize(
num bytes, {int precision = 0, int? maxPrecision}) → String -
Formats
bytesas a human-readable file size. -
floor(
num value, [int precision = 0]) → double -
Floors
valuetoprecisiondecimal 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
numberwith locale-aware separators. -
gcd(
int a, int b) → int -
Greatest common divisor of
aandb. -
lcm(
int a, int b) → int -
Least common multiple of
aandb. -
lerp(
num a, num b, double t) → double -
Linearly interpolates between
aandbbyt(0..1). -
max(
Iterable< num> values) → num -
Returns the largest value in
values. -
median(
Iterable< num> values) → double -
Returns the median of
values, or0if 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 byby. -
parseFloat(
String value, {String? locale}) → double? -
Parses
valueas a double, locale-aware. Returns null on failure. -
parseInt(
String value, {String? locale}) → int? -
Parses
valueas an int, locale-aware. Returns null on failure. -
percentage(
num number, {int precision = 0, int? maxPrecision, String? locale}) → String -
Formats
numberas a percentage. The number is treated as the whole percentage value:10produces'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) toend(inclusive), stepping bystep. -
round(
num value, [int precision = 0]) → double -
Rounds
valuetoprecisiondecimal places. -
scale(
num value, {required num fromMin, required num fromMax, required num toMin, required num toMax}) → double -
Re-maps
valuefrom one range to another. -
spell(
int number) → String -
Spells out
numberin English. -
spellOrdinal(
int number) → String -
Spells out the ordinal form of
numberin 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.