Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Calculate number of digits of a decimal number in c

how am I supposed to find the number of digits of decimal numbers in c?
ex. find how many digits are in 123.4567?!

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

how am I supposed to find the number of digits of decimal numbers in
c? ex. find how many digits are in 123.4567?

Start by converting it to a string.

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

int main() {
    char input[] = "123.456";

    int i, count = 0, len = strlen(input);
    for (i = 0; i < len; i++) {
        if (input[i] == '.') {
            printf("int count %d\n", count);
            continue;
        }
        if (isdigit(input[i])) {
            count++;
            continue;
        }
        // Found a non-digit non-decimal point.
        break;
    }
    printf("all count %d\n", count);
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading