From 5693617912ee42de83e938c86aa570bf321debd8 Mon Sep 17 00:00:00 2001 From: Chris Date: Wed, 10 Sep 2025 19:46:07 +0200 Subject: [PATCH] trim to 2 decimals --- util/format.h | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/util/format.h b/util/format.h index de5ae5a..242244f 100644 --- a/util/format.h +++ b/util/format.h @@ -1,5 +1,6 @@ #pragma once #include +#include #include #include #include @@ -27,10 +28,16 @@ inline std::string format(const char* fmt, ...) return std::string(buf.data(), needed); } -inline std::string to_string_trimmed(double value) +inline std::string float_to_string_trimmed(float value) { - std::ostringstream oss; - oss << std::defaultfloat << value; - return oss.str(); + std::ostringstream out; + out << std::fixed << std::setprecision(2) << value; + std::string str = out.str(); + + // Remove trailing zeros + str.erase(str.find_last_not_of('0') + 1, std::string::npos); + // If the last character is a decimal point, remove it as well + if (!str.empty() && str.back() == '.') { str.pop_back(); } + return str; } } // namespace trnr \ No newline at end of file