Используйте цикл for, как показано здесь:
Код: Выделить всё
int length = 1;
int x = 234567545;
while (x /= 10)
length++;
Код: Выделить всё
uint32_t x{234567};
double ds = std::log10(static_cast(x)) + 1;
int digits = static_cast(ds);
Вот мой код:
Демо
Код: Выделить всё
#include
#include
#include
#include
#include
using allocator_t = std::pmr::polymorphic_allocator;
template
inline auto to_string(T number, allocator_t allocator = {}) -> std::pmr::string {
// const std::size_t size = ???
std::pmr::string str{ size, '\0', allocator };
if constexpr(std::same_as) {
std::snprintf(&str.front(), size, "%" PRIu32, number);
} else if constexpr (std::same_as) {
std::snprintf(&str.front(), size, "%" PRIu16, number);
} else if constexpr (std::same_as) {
std::snprintf(&str.front(), size, "%" PRIu8, number);
}
// ...
return str;
}
int main()
{
uint32_t x = 256;
printf("My number = %s\n", to_string(x).data());
}
Подробнее здесь: https://stackoverflow.com/questions/755 ... 10-integer