可以使用 sprintf 函数将实数转换为字符串,然后使用 strlen 函数计算整数部分的位数。

示例代码:

#include <stdio.h>
#include <string.h>

int main() {
    double num = 123.456789;
    char str[100];
    sprintf(str, '%.15g', num);  // 将实数转换为字符串
    int len = strcspn(str, '.');  // 计算整数部分的位数
    printf('整数部分的位数为%d\n', len);
    return 0;
}

输出:

整数部分的位数为3
C语言获取实数整数部分位数的技巧

原文地址: https://www.cveoy.top/t/topic/mW3w 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录