67 lines
1.8 KiB
C++
67 lines
1.8 KiB
C++
/*
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
|
*/
|
|
|
|
#include "UnitFormatter.hpp"
|
|
#include <string>
|
|
#include <sstream>
|
|
#include <iomanip>
|
|
|
|
namespace
|
|
{
|
|
template<typename T> static std::string formatValue(T value, int precision)
|
|
{
|
|
std::ostringstream stream;
|
|
stream << std::fixed << std::setprecision(precision) << value;
|
|
return stream.str();
|
|
}
|
|
}
|
|
|
|
namespace OpenVulkano
|
|
{
|
|
UnitFormatter::UnitFormatter(bool useMetric, int precisionDigits) : metric(useMetric), precision(precisionDigits) {}
|
|
|
|
std::string UnitFormatter::Format(units::length::meter_t distance)
|
|
{
|
|
if (metric)
|
|
{
|
|
if (distance >= units::length::kilometer_t(1))
|
|
{
|
|
return formatValue(units::length::kilometer_t(distance).value(), precision) + " km";
|
|
}
|
|
return formatValue(distance.value(), precision) + " m";
|
|
}
|
|
else
|
|
{
|
|
auto distanceFeet = units::length::foot_t(distance).value();
|
|
if (distanceFeet >= 5280.0)
|
|
{
|
|
return formatValue(units::length::mile_t(distance).value(), precision) + " mi";
|
|
}
|
|
return formatValue(distanceFeet, precision) + " ft";
|
|
}
|
|
}
|
|
|
|
std::string UnitFormatter::Format(units::area::square_meter_t area)
|
|
{
|
|
if (metric)
|
|
{
|
|
if (area >= units::area::square_kilometer_t(1))
|
|
{
|
|
return formatValue(units::area::square_kilometer_t(area).value(), precision) + " km²";
|
|
}
|
|
return formatValue(area.value(), precision) + " 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(areaSquareFeet, precision) + " ft²";
|
|
}
|
|
}
|
|
} |