bestFor static method
Determines the best unit for given amount and type.
Unit shouldBeKibibyte = Unit.bestFor(2000, UnitType.binary);
amount
is the only required parameter. If type
is not provided,
will default to returning a binary based unit.
Will throw ArgumentError if amount is a non finite value. That is double.infinity, double.negativeInfinity or double.nan.
Implementation
static Unit bestFor(double amount, [UnitType type = UnitType.binary]) {
if (!amount.isFinite) {
throw ArgumentError.value(amount, 'amount', 'Expected a finite value.');
}
var positive = amount.abs();
if (positive == 0) {
return Unit.byte;
}
return _getUnitListForType(type)
.firstWhere((unit) => (unit.multiplier <= positive));
}