Made Format methods const and nodiscard, passing suffix to the Format function
This commit is contained in:
@@ -11,7 +11,8 @@
|
|||||||
|
|
||||||
namespace
|
namespace
|
||||||
{
|
{
|
||||||
template<typename T> std::string FormatValue(T value, int precision, bool trimTrailingZeros = false)
|
template<typename T>
|
||||||
|
std::string FormatValue(T value, int precision, bool trimTrailingZeros, const std::string& suffix)
|
||||||
{
|
{
|
||||||
std::ostringstream out;
|
std::ostringstream out;
|
||||||
out << std::fixed << std::setprecision(precision) << value;
|
out << std::fixed << std::setprecision(precision) << value;
|
||||||
@@ -26,7 +27,7 @@ namespace
|
|||||||
result.pop_back();
|
result.pop_back();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
result += suffix;
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -38,19 +39,19 @@ namespace OpenVulkano
|
|||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string UnitFormatter::Format(units::length::meter_t distance)
|
std::string UnitFormatter::Format(units::length::meter_t distance) const
|
||||||
{
|
{
|
||||||
if (metric)
|
if (metric)
|
||||||
{
|
{
|
||||||
if (distance > units::length::meter_t(0) && distance < units::length::meter_t(0.1))
|
if (distance > units::length::meter_t(0) && distance < units::length::meter_t(0.1))
|
||||||
{
|
{
|
||||||
return FormatValue(units::length::millimeter_t(distance).value(), precision, trimTrailingZeros) + " mm";
|
return FormatValue(units::length::millimeter_t(distance).value(), precision, trimTrailingZeros, " mm");
|
||||||
}
|
}
|
||||||
else if (distance >= units::length::kilometer_t(1))
|
else if (distance >= units::length::kilometer_t(1))
|
||||||
{
|
{
|
||||||
return FormatValue(units::length::kilometer_t(distance).value(), precision, trimTrailingZeros) + " km";
|
return FormatValue(units::length::kilometer_t(distance).value(), precision, trimTrailingZeros, " km");
|
||||||
}
|
}
|
||||||
return FormatValue(distance.value(), precision, trimTrailingZeros) + " m";
|
return FormatValue(distance.value(), precision, trimTrailingZeros, " m");
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@@ -59,35 +60,34 @@ namespace OpenVulkano
|
|||||||
|
|
||||||
if (distanceFeet > 0 && distanceFeet < 0.1)
|
if (distanceFeet > 0 && distanceFeet < 0.1)
|
||||||
{
|
{
|
||||||
return FormatValue(distanceInches, precision, trimTrailingZeros) + " in";
|
return FormatValue(distanceInches, precision, trimTrailingZeros, " in");
|
||||||
}
|
}
|
||||||
else if (distanceFeet >= 5280.0)
|
else if (distanceFeet >= 5280.0)
|
||||||
{
|
{
|
||||||
return FormatValue(units::length::mile_t(distance).value(), precision, trimTrailingZeros) + " mi";
|
return FormatValue(units::length::mile_t(distance).value(), precision, trimTrailingZeros, " mi");
|
||||||
}
|
}
|
||||||
return FormatValue(distanceFeet, precision, trimTrailingZeros) + " ft";
|
return FormatValue(distanceFeet, precision, trimTrailingZeros, " ft");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string UnitFormatter::Format(units::area::square_meter_t area)
|
std::string UnitFormatter::Format(units::area::square_meter_t area) const
|
||||||
{
|
{
|
||||||
if (metric)
|
if (metric)
|
||||||
{
|
{
|
||||||
if (area >= units::area::square_kilometer_t(1))
|
if (area >= units::area::square_kilometer_t(1))
|
||||||
{
|
{
|
||||||
return FormatValue(units::area::square_kilometer_t(area).value(), precision, trimTrailingZeros)
|
return FormatValue(units::area::square_kilometer_t(area).value(), precision, trimTrailingZeros, " km²");
|
||||||
+ " km²";
|
|
||||||
}
|
}
|
||||||
return FormatValue(area.value(), precision, trimTrailingZeros) + " m²";
|
return FormatValue(area.value(), precision, trimTrailingZeros, " m²");
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
auto areaSquareFeet = units::area::square_foot_t(area).value();
|
auto areaSquareFeet = units::area::square_foot_t(area).value();
|
||||||
if (areaSquareFeet >= 27878400.0)
|
if (areaSquareFeet >= 27878400.0)
|
||||||
{
|
{
|
||||||
return FormatValue(units::area::square_mile_t(area).value(), precision, trimTrailingZeros) + " mi²";
|
return FormatValue(units::area::square_mile_t(area).value(), precision, trimTrailingZeros, " mi²");
|
||||||
}
|
}
|
||||||
return FormatValue(areaSquareFeet, precision, trimTrailingZeros) + " ft²";
|
return FormatValue(areaSquareFeet, precision, trimTrailingZeros, " ft²");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -18,7 +18,7 @@ namespace OpenVulkano
|
|||||||
|
|
||||||
public:
|
public:
|
||||||
UnitFormatter(bool useMetric = true, int precisionDigits = 3, bool doTrimTrailingZeros = true);
|
UnitFormatter(bool useMetric = true, int precisionDigits = 3, bool doTrimTrailingZeros = true);
|
||||||
std::string Format(units::length::meter_t distance);
|
[[nodiscard]] std::string Format(units::length::meter_t distance) const;
|
||||||
std::string Format(units::area::square_meter_t area);
|
[[nodiscard]] std::string Format(units::area::square_meter_t area) const;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user