Option to trim zeros at the end, more tests
This commit is contained in:
@@ -11,36 +11,61 @@
|
||||
|
||||
namespace
|
||||
{
|
||||
template<typename T> static std::string formatValue(T value, int precision)
|
||||
template<typename T> std::string FormatValue(T value, int precision, bool trimTrailingZeros = false)
|
||||
{
|
||||
std::ostringstream stream;
|
||||
stream << std::fixed << std::setprecision(precision) << value;
|
||||
return stream.str();
|
||||
std::ostringstream out;
|
||||
out << std::fixed << std::setprecision(precision) << value;
|
||||
std::string result = out.str();
|
||||
|
||||
if (trimTrailingZeros && result.find('.') != std::string::npos)
|
||||
{
|
||||
result.erase(result.find_last_not_of('0') + 1);
|
||||
|
||||
if (result.back() == '.')
|
||||
{
|
||||
result.pop_back();
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
namespace OpenVulkano
|
||||
{
|
||||
UnitFormatter::UnitFormatter(bool useMetric, int precisionDigits) : metric(useMetric), precision(precisionDigits) {}
|
||||
UnitFormatter::UnitFormatter(bool useMetric, int precisionDigits, bool doTrimTrailingZeros)
|
||||
: metric(useMetric), precision(precisionDigits), trimTrailingZeros(doTrimTrailingZeros)
|
||||
{
|
||||
}
|
||||
|
||||
std::string UnitFormatter::Format(units::length::meter_t distance)
|
||||
{
|
||||
if (metric)
|
||||
{
|
||||
if (distance >= units::length::kilometer_t(1))
|
||||
if (distance > units::length::meter_t(0) && distance < units::length::meter_t(0.1))
|
||||
{
|
||||
return formatValue(units::length::kilometer_t(distance).value(), precision) + " km";
|
||||
return FormatValue(units::length::millimeter_t(distance).value(), precision, trimTrailingZeros) + " mm";
|
||||
}
|
||||
return formatValue(distance.value(), precision) + " m";
|
||||
else if (distance >= units::length::kilometer_t(1))
|
||||
{
|
||||
return FormatValue(units::length::kilometer_t(distance).value(), precision, trimTrailingZeros) + " km";
|
||||
}
|
||||
return FormatValue(distance.value(), precision, trimTrailingZeros) + " m";
|
||||
}
|
||||
else
|
||||
{
|
||||
auto distanceFeet = units::length::foot_t(distance).value();
|
||||
if (distanceFeet >= 5280.0)
|
||||
auto distanceInches = units::length::inch_t(distance).value();
|
||||
|
||||
if (distanceFeet > 0 && distanceFeet < 0.1)
|
||||
{
|
||||
return formatValue(units::length::mile_t(distance).value(), precision) + " mi";
|
||||
return FormatValue(distanceInches, precision, trimTrailingZeros) + " in";
|
||||
}
|
||||
return formatValue(distanceFeet, precision) + " ft";
|
||||
else if (distanceFeet >= 5280.0)
|
||||
{
|
||||
return FormatValue(units::length::mile_t(distance).value(), precision, trimTrailingZeros) + " mi";
|
||||
}
|
||||
return FormatValue(distanceFeet, precision, trimTrailingZeros) + " ft";
|
||||
}
|
||||
}
|
||||
|
||||
@@ -50,18 +75,19 @@ namespace OpenVulkano
|
||||
{
|
||||
if (area >= units::area::square_kilometer_t(1))
|
||||
{
|
||||
return formatValue(units::area::square_kilometer_t(area).value(), precision) + " km²";
|
||||
return FormatValue(units::area::square_kilometer_t(area).value(), precision, trimTrailingZeros)
|
||||
+ " km²";
|
||||
}
|
||||
return formatValue(area.value(), precision) + " m²";
|
||||
return FormatValue(area.value(), precision, trimTrailingZeros) + " m²";
|
||||
}
|
||||
else
|
||||
{
|
||||
auto areaSquareFeet = units::area::square_foot_t(area).value();
|
||||
if (areaSquareFeet >= 27878400.0)
|
||||
{
|
||||
return formatValue(units::area::square_mile_t(area).value(), precision) + " mi²";
|
||||
return FormatValue(units::area::square_mile_t(area).value(), precision, trimTrailingZeros) + " mi²";
|
||||
}
|
||||
return formatValue(areaSquareFeet, precision) + " ft²";
|
||||
return FormatValue(areaSquareFeet, precision, trimTrailingZeros) + " ft²";
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -14,9 +14,10 @@ namespace OpenVulkano
|
||||
{
|
||||
bool metric = true;
|
||||
int precision = 3;
|
||||
bool trimTrailingZeros = true;
|
||||
|
||||
public:
|
||||
UnitFormatter(bool useMetric = true, int precisionDigits = 3);
|
||||
UnitFormatter(bool useMetric = true, int precisionDigits = 3, bool doTrimTrailingZeros = true);
|
||||
std::string Format(units::length::meter_t distance);
|
||||
std::string Format(units::area::square_meter_t area);
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user