MCPcopy Create free account
hub / github.com/comaps/comaps / GetFormattedDistance

Method GetFormattedDistance

libs/platform/distance.cpp:164–196  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

162}
163
164Distance Distance::GetFormattedDistance() const
165{
166 ASSERT(IsValid(), ());
167
168 // To low units.
169 Distance res;
170 if (m_units == Units::Kilometers)
171 res = To(Units::Meters);
172 else if (m_units == Units::Miles)
173 res = To(Units::Feet);
174 else
175 res = *this;
176
177 double lowRound = std::round(res.m_distance);
178 // Round distances over 100 units to 10 units, e.g. 112 -> 110, 998 -> 1000
179 if (lowRound > 100)
180 lowRound = std::round(lowRound / 10) * 10;
181
182 // Use high units for distances of 1000 units and over,
183 // e.g. 1000m -> 1.0km, 1290m -> 1.3km, 1000ft -> 0.2mi
184 if (lowRound >= 1000.0)
185 {
186 // To high units.
187 res = res.To(res.m_units == Units::Meters ? Units::Kilometers : Units::Miles);
188
189 // For distances of 10.0 high units and over round to a whole number, e.g. 9.98 -> 10, 10.9 -> 11
190 uint8_t const precision = (std::round(res.m_distance * 10) / 10 >= 10.0) ? 0 : 1;
191 return {WithPrecision(res.m_distance, precision), res.m_units};
192 }
193
194 res.m_distance = lowRound;
195 return res;
196}
197
198std::string Distance::ToString() const
199{

Callers 2

UNIT_TESTFunction · 0.80

Calls 4

ASSERTFunction · 0.85
WithPrecisionFunction · 0.85
IsValidFunction · 0.50
ToMethod · 0.45

Tested by 1

UNIT_TESTFunction · 0.64