Method 1
#include <cmath>
int countDecimalPlaces(double num)
{
if (num == 0) return 0;
return std::floor(std::log10(std::abs(num - std::floor(num)))) + 1;
}
Method 2
#include <string>
#include <sstream>
int countDecimalPlaces(double num)
{
std::ostringstream oss;
oss << std::fixed << num;
std::string str = oss.str();
return str.find('.') - str.find('e') > 0 ? str.find('.') + 1 : str.find('.');
}
Method 3
#include <cmath>
int countDecimalPlaces(double num)
{
if (num == 0) return 0;
return std::abs(std::modf(num, nullptr)) == 0 ? 0 : std::floor(std::log10(std::abs(std::modf(num, nullptr)))) + 1;
}