How to count the digits of float in C

1 Answer

0 votes
#include <stdio.h>

int count_digits_of_float(float f) {
    char str[32] = "";

    sprintf(str, "%f", f);
    puts(str);

    int i = 0;
    while (str[i]) {
        i++;
    }
    
    return i - 1; 
}

int main(void)
{
    float f = 293.140592;

    int total_digits = count_digits_of_float(f);

    printf("%d\n", total_digits);
      
    return 0;
}

    
    
/*
run:
 
293.140594
9
 
*/

 



answered Jul 21, 2024 by avibootz
edited Jul 21, 2024 by avibootz

Related questions

2 answers 113 views
113 views asked Nov 10, 2023 by avibootz
1 answer 164 views
1 answer 97 views
1 answer 120 views
1 answer 103 views
...