bestFor static method

Unit bestFor(
  1. double amount, [
  2. UnitType type = UnitType.binary
])

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));
}