Getting the number of decimal places: Difference between revisions

Line 44:
12.3450 has 4 decimals
</pre>
 
=={{header|C}}==
<lang c>#include <stdio.h>
 
int findNumOfDec(double x) {
char buffer[128];
int pos, num;
 
sprintf(buffer, "%.14f", x);
 
pos = 0;
num = 0;
while (buffer[pos] != 0 && buffer[pos] != '.') {
pos++;
}
if (buffer[pos] != 0) {
pos++; // skip over the decimal
while (buffer[pos] != 0) {
pos++; // find the end of the string
}
pos--; //reverse past the null sentiel
while (buffer[pos] == '0') {
pos--; // reverse past any zeros
}
while (buffer[pos] != '.') {
num++;
pos--; // only count decimals from this point
}
}
return num;
}
 
void test(double x) {
int num = findNumOfDec(x);
printf("%f has %d decimals\n", x, num);
}
 
int main() {
test(12.0);
test(12.345);
test(12.345555555555);
test(12.3450);
test(12.34555555555555555555);
test(1.2345e+54);
return 0;
}</lang>
{{out}}
<pre>12.000000 has 0 decimals
12.345000 has 3 decimals
12.345556 has 12 decimals
12.345000 has 3 decimals
12.345556 has 14 decimals
1234500000000000060751116919315055127939946206157864960.000000 has 0 decimals</pre>
 
=={{header|Go}}==
1,452

edits