convertFrequency function Null safety

double convertFrequency(
  1. Frequency from,
  2. Frequency to,
  3. double value
)

Convert frequencies. Some decimals may be lost in the proccess.

from, to and value can NOT be null, otherwise an AssertionError is thrown

Basic usage:

main() {
  final frequency = convertEnergy(
    Frequency.megahertz, // from
    Frequency.hertz, // to
    1, // value
  );
  print(frequency); // 1000000
}

Read the documentation for more information

Implementation

double convertFrequency(Frequency from, Frequency to, double value) {
  verify(from, to, value);
  if (from == to) return value;
  if (from == Frequency.hertz) {
    switch (to) {
      case Frequency.gigahertz:
        return value / 1e+9;
      case Frequency.kilohertz:
        return value / 1000;
      case Frequency.megahertz:
        return value / 1e+6;
      default:
        return value;
    }
  } else {
    final hertz = Frequency.hertz;
    switch (from) {
      case Frequency.gigahertz:
        return convertFrequency(hertz, to, value * 1e+9);
      case Frequency.kilohertz:
        return convertFrequency(hertz, to, value * 1000);
      case Frequency.megahertz:
        return convertFrequency(hertz, to, value * 1e+6);
      default:
        return value;
    }
  }
}