You can convert the double value to string:
double value = 123456.05;
std::string s = std::to_string(value);
After it you need to remove trailing zeroez (because it is possible that s == "123456.050000"
now):
s.erase(s.find_last_not_of('0') + 1, std::string::npos);
Than get a number of characters of this string:
std::cout<<"number of digit::"<< s.length();
(In this case you will process "." as digit)
So, the program below returns expected result:
number of digit::9
But this is not perfect for integer values, because "123" will be represented as "123." (one more character at the end). So, for integer values it is better to remove trailing "." before getting of the length:
void print_nb_digits(double value) {
std::string s = std::to_string(value);
s.erase(s.find_last_not_of('0') + 1, std::string::npos);
if (!s.empty() && !std::isdigit(s.back()))
s.pop_back();
std::cout<<"number of digit::"<< s.length();
}
double value = 123456.05;
print_nb_digits(value);
In this case the program returns correct result for value = 123456
too:
number of digit::6