diff --git a/src/include/units/bits/unit_text.h b/src/include/units/bits/unit_text.h index f10ab0ff..95421d51 100644 --- a/src/include/units/bits/unit_text.h +++ b/src/include/units/bits/unit_text.h @@ -178,4 +178,14 @@ constexpr auto unit_text() } } +template +void to_stream(std::basic_ostream& os, const quantity& q) +{ + os << q.count(); + constexpr auto symbol = detail::unit_text(); + if constexpr (symbol.standard().size()) { + os << " " << symbol.standard(); + } +} + } // namespace units::detail diff --git a/src/include/units/quantity.h b/src/include/units/quantity.h index 57b84410..72e53405 100644 --- a/src/include/units/quantity.h +++ b/src/include/units/quantity.h @@ -334,11 +334,14 @@ public: template friend std::basic_ostream& operator<<(std::basic_ostream& os, const quantity& q) { - os << q.count(); - constexpr auto symbol = detail::unit_text(); - if constexpr (symbol.standard().size()) { - os << " " << symbol.standard(); + if(os.width()) { + // std::setw() applies to the whole quantity output so it has to be first put into std::string + std::basic_ostringstream s; + detail::to_stream(s, q); + return os << s.str(); } + + detail::to_stream(os, q); return os; } };