Skip to content

Commit

Permalink
remove trailing zeros only if the container is a floating point type (#…
Browse files Browse the repository at this point in the history
…274)

* remove trailing zeros only if the container is a floating point type

* silence warning

Co-authored-by: Stefan Erl <stefan.erl@dlr.de>
  • Loading branch information
st-e and Stefan Erl committed Mar 1, 2021
1 parent 702c543 commit 90767e9
Showing 1 changed file with 13 additions and 9 deletions.
22 changes: 13 additions & 9 deletions include/units/core.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,17 +83,21 @@ namespace units
std::string to_string(const T& t)
{
std::string str{std::to_string(t)};
unsigned int offset{1};

// remove trailing decimal points for integer value units. Locale aware!
struct std::lconv* lc;
lc = std::localeconv();
char decimalPoint = *lc->decimal_point;
if (str.find_last_not_of('0') == str.find(decimalPoint))
if constexpr (std::is_floating_point_v<T>)
{
offset = 0;
unsigned int offset{1};

// remove trailing decimal points for integer value units. Locale aware!
struct std::lconv* lc;
lc = std::localeconv();
char decimalPoint = *lc->decimal_point;
if (str.find_last_not_of('0') == str.find(decimalPoint))
{
offset = 0;
}
str.erase(str.find_last_not_of('0') + offset, std::string::npos);
}
str.erase(str.find_last_not_of('0') + offset, std::string::npos);
return str;
}
} // namespace detail
Expand Down Expand Up @@ -1707,7 +1711,7 @@ namespace units
namespace detail
{
template<unsigned long long Exp, typename B>
constexpr auto pow_acc(B acc, B base) noexcept
constexpr auto pow_acc(B acc, B base [[maybe_unused]]) noexcept
{
if constexpr (Exp == 0)
{
Expand Down

0 comments on commit 90767e9

Please sign in to comment.