toHumanReadableSpeedCount static method
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 metric: indicates the unit system: metric or imperial system @param locale: indicates the user locale to get the proper unit system: metric or imperial system @param context: indicates the build context to be used to get the system locale. This will be used if locale param is null @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,
{bool? metric, Locale? locale, BuildContext? context, int decimals = 2}) {
metric ??= (locale ??=
FlutterViewUtils.get(context: context).platformDispatcher.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);
}