文章目录 Method 1Method 2Method 3 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; }