Use a simpler way of formatting floats

This commit is contained in:
Evil Eye 2025-08-16 11:33:58 +02:00
parent eb01a302f1
commit b7997a5e88
2 changed files with 10 additions and 48 deletions

View File

@ -30,50 +30,6 @@
#include "itemmodel.hpp"
namespace
{
template <unsigned short Precision = 2>
struct LimitedFloat
{
float mValue;
constexpr unsigned short getPrecision() { return Precision; }
};
}
template <>
struct std::formatter<LimitedFloat<>, char>
{
template <class ParseContext>
constexpr ParseContext::iterator parse(ParseContext& ctx)
{
return ctx.begin();
}
template <class FmtContext>
FmtContext::iterator format(LimitedFloat<> v, FmtContext& ctx) const
{
constexpr unsigned short precision = v.getPrecision();
// FLOAT_MAX needs 39 digits + 1 for the sign + 1 for the decimal separator
constexpr std::size_t bufferSize = 41 + precision;
char buffer[bufferSize];
std::to_chars_result result
= std::to_chars(buffer, buffer + bufferSize, v.mValue, std::chars_format::fixed, precision);
if (result.ec != std::errc())
{
// Should never happen, but just default to regular float formatting
return std::format_to(ctx.out(), "{}", v.mValue);
}
// Trim result so 1.00 turns into 1
char* end = result.ptr;
while (end > buffer && *(end - 1) == '0')
--end;
if (end > buffer && *(end - 1) == '.')
--end;
return std::ranges::copy(buffer, end, ctx.out()).out;
}
};
namespace MWGui
{
ToolTips::ToolTips()
@ -687,7 +643,13 @@ namespace MWGui
std::string ToolTips::toString(const float value)
{
return std::format("{}", LimitedFloat(value));
std::string s = std::format("{:.2f}", value);
// Trim result so 1.00 turns into 1
while (!s.empty() && s.back() == '0')
s.pop_back();
if (!s.empty() && s.back() == '.')
s.pop_back();
return s;
}
std::string ToolTips::toString(const int value)
@ -699,14 +661,14 @@ namespace MWGui
{
if (weight == 0)
return {};
return std::format("\n{}: {}", prefix, LimitedFloat(weight));
return std::format("\n{}: {}", prefix, toString(weight));
}
std::string ToolTips::getPercentString(const float value, std::string_view prefix)
{
if (value == 0)
return {};
return std::format("\n{}: {}%", prefix, LimitedFloat(value * 100));
return std::format("\n{}: {}%", prefix, toString(value * 100));
}
std::string ToolTips::getValueString(const int value, std::string_view prefix)

View File

@ -16,7 +16,7 @@ namespace MWGui
EXPECT_EQ(ToolTips::toString(1234567.12f), "1234567.12");
EXPECT_EQ(ToolTips::toString(0.001f), "0");
EXPECT_EQ(ToolTips::toString(0.01f), "0.01");
EXPECT_EQ(ToolTips::toString(0.01f), "0.01");
EXPECT_EQ(ToolTips::toString(0.012f), "0.01");
EXPECT_EQ(ToolTips::toString(std::numeric_limits<float>::infinity()), "inf");
EXPECT_EQ(ToolTips::toString(std::numeric_limits<float>::quiet_NaN()), "nan");
}