toHumanReadableSpeedCount static method

String toHumanReadableSpeedCount(
  1. double speed,
  2. int time, {
  3. Locale? locale,
  4. int decimals = 2,
})

The formula is speed m/time where the speed should be in meters and time is specified in seconds base for instance: to indicate 1000 m/seconds the call should be (1000, 1) to indicate 1000 m/minute the call should be (1000, 60) to indicate 1000 m/hour the call should be (1000, 3600) @param speed: in meters @param time: in seconds has to be 1s or 60s or 3600s; that indicates seconds, minute, hour @param locale: indicates the user locale to get the proper unit system: metric or imperial system @param decimals: amount of decimals after float point @return formatted: speed in km/h or mi/h according to metric as String

Implementation

static String toHumanReadableSpeedCount(double speed, int time,
    {Locale? locale, int decimals = 2}) {
  bool metric = (locale ??= window.locale).usesMetricSystem;
  if (time == 1) {
    time = 3600;
  } else if (time == 60) {
    time = 60;
  } else if (time == 3600) {
    time = 1;
  } else {
    time = 1;
  }

  speed = speed * time.toDouble() / (metric ? 1000 : 1609.344).toDouble();
  String decimalsParam = '';
  if (decimals > 0) decimalsParam = '0.';
  while (decimals-- > 0) {
    decimalsParam += '0';
  }
  String unit = metric ? 'km' : 'mi';
  return Intl.NumberFormat('#$decimalsParam $unit/h', locale.toString())
      .format(speed);
}