[platform] Make distance rounding more consistent

E.g. both 998m and 1004m are rounded to 1km now
(before the change 998m rounded to 1000m).
Likewise 9992m is rounded to 10km now, not 10.0km.

Signed-off-by: Konstantin Pastbin <konstantin.pastbin@gmail.com>
This commit is contained in:
Konstantin Pastbin 2021-12-15 17:07:12 +03:00 committed by Viktor Govako
parent d302387764
commit 61d76b31a7

View file

@ -39,19 +39,20 @@ string FormatDistanceImpl(Units units, double m, string const & low, string cons
case Units::Metric: highF = 1000.0; lowF = 1.0; break;
}
double const lowV = m / lowF;
if (lowV < 1.0)
return string("0 ") + low;
double lowV = round(m / lowF);
// Round distances over 100 units to 10 units, e.g. 112 -> 110, 998 -> 1000
lowV = lowV <= 100.0 ? lowV : round(lowV / 10) * 10;
// To display any lower units only if < 1000
if (m >= 1000.0 * lowF)
// Use high units for distances of 1000 units and over,
// e.g. 1000m -> 1.0km, 1290m -> 1.3km, 1000ft -> 0.2mi
if (lowV >= 1000.0)
{
double const v = m / highF;
return ToStringPrecision(v, v >= 10.0 ? 0 : 1) + " " + high;
double const highV = m / highF;
// For distances of 10.0 high units and over round to a whole number, e.g. 9.98 -> 10, 10.9 -> 11
return ToStringPrecision(highV, round(highV * 10) / 10 >= 10.0 ? 0 : 1) + " " + high;
}
// To display unit number only if <= 100.
return ToStringPrecision(lowV <= 100.0 ? lowV : round(lowV / 10) * 10, 0) + " " + low;
return ToStringPrecision(lowV, 0) + " " + low;
}
string FormatAltitudeImpl(Units units, double altitude, string const & localizedUnits)