Improve precision handling

This commit is contained in:
Georg Hagen
2025-01-10 00:11:51 +01:00
parent 1b0d59940b
commit 97d05804ee

View File

@@ -15,7 +15,17 @@ namespace
std::string FormatValue(T value, int precision, bool trimTrailingZeros, const std::string& suffix)
{
std::ostringstream out;
out << std::fixed << std::setprecision(precision) << value;
if (precision >= 0)
{
out << std::fixed << std::setprecision(precision) << value;
}
else
{
int p = -precision;
T v = value;
while (v > 1 && p > 0) { v /= 10; p--; }
out << std::fixed << std::setprecision(p) << value;
}
std::string result = out.str();
if (trimTrailingZeros && result.find('.') != std::string::npos)