Originaler README-Schnappschuss aus dem verknüpften Quell-Repository. Aktuelle Inhalte finden Sie im Repository.
num_utilities
A collection of extension methods for [num]s, [int]s, and [double]s;
and iterables of [num]s, [int]s, and [double]s.
See: list_utilities
Usage
import 'package:num_utilities/num_utilities.dart';
Numbers
[num]s, [int]s, and [double]s were extended with the
following methods/getters:
isPositive
[isPositive] returns true if the number's sign is positive.
final numA = 5;
print(numA.isPositive);
final numB = -5;
print(numB.isPositive);
equals
[equals] is equivalent to the == operator, but has an optional parameter
to round the numbers before comparing them, by calling [roundTo] (see below.)
final numA = 5.556;
final numB = 5.557;
print(numA.equals(numB));
print(numA.equals(numB, 100));
isWithin
[isWithin] returns true if the number matches or is within the range
of any of the parts defined in the provided [expression].
The provided [expression] must be a string of numbers and/or ranges
separated by commas.
final value = 7;
print(value.isWithin('0,2,4,6'));
print(value.isWithin('1,2,6-8,10'));
print(value.isWithin('0-4,7,12-15'));
isWithinRange
[isWithinRange] returns true if this number is within the range of the
provided [start] and [end] values.
final value = 7;
print(value.isWithinRange(4, 8));
print(value.isWithinRange(8, 4));
print(value.isWithinRange(2, 6));
roundTo
[roundTo] rounds the number by the value: (number * value).round() / value
final number = 5.556;
print(number.roundTo(10));
roundToPrecision
[roundToPrecision] rounds the number to the specified number of decimal points.
final number = 5.5567893;
print(number.roundToPrecision(3));
Iterables
Iterables of [num]s, [int]s, and [double]s were extended
with the following methods:
sum
The [sum] method returns the sum of all of the values in the iterable.
final list = [1, 2, 3, 4, 5];
print(list.sum());
absSum
The [absSum] method returns the sum of all of the absolute values in the iterable.
[absSum] has an optional parameter to invert the returned value.
final list = [1, -2, 3, -4, 5];
print(list.absSum());
print(list.absSum(false));
print(list.absSum(true));
print(list.sum());
highest
The [highest] getter returns the highest value in the iterable.
final list = [1, 2, 3, 4, 5];
print(list.highest);
lowest
The [lowest] getter returns the lowest value in the iterable.
final list = [1, 2, 3, 4, 5];
print(list.lowest);
Range
The [Range] object defines a range of numbers between specified
[min] and [max] values, and has convienence methods for generating
numbers within the range and for interpolating values within the range.
final range = Range(5, 15);
print(range.min);
print(range.max);
span
The [span] getter returns the difference between the [min] and [max] values.
print(range.span);
random
The [random] method will output a number between the [min] value, inclusive,
and the [max] value, exclusive.
[random] accepts an optional [seed] value.
print(range.random());
print(range.random(seed));
secureRandom
The [secureRandom] method will output a cryptographically secure number
between the [min] value, inclusive, and [max] value, exclusive.
print(range.secureRandom());
interpolate
The [interpolate] method returns a [List] of numbers spanning a
defined number of [steps] between the [min] and [max] values.
print(range.interpolate(5));
inclusive
[interpolate]'s inclusive parameter can be used to include the
[max] number within the output [List].
print(range.interpolate(5, inclusive: true));
randomize
[interpolate]'s randomize parameter can be used to apply randomness
to the output numbers, where a value of 0.0 doesn't apply any randomness
to the numbers, while a value of 1.0 will randomize the output numbers
within the entire range of the radius around each step.
print(range.interpolate(5, randomize: 1.0));