how am I supposed to find the number of digits of decimal numbers in c? ex. find how many digits are in 123.4567?!
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, d = 0, count = 0, len = strlen(input);
for (i = 0; i < len; i++) {
if (input[i] == '.') {
if (++d == 1) {
printf("int count %d\n", count);
if (i == 0) {
break;
}
continue;
}
// 2 or more decimal points.
break;
}
if (isdigit(input[i])) {
count++;
continue;
}
// Found a non-digit non-decimal point.
if (i == 0) {
printf("int count %d\n", count);
}
break;
}
printf("all count %d\n", count);
}