Getting the number of decimal places: Difference between revisions

→‎{{header|C}}: Changed to be in accordance with task description.
(add RPL)
(→‎{{header|C}}: Changed to be in accordance with task description.)
Line 167:
 
=={{header|C}}==
{{Template:Incorrect}}
<syntaxhighlight lang="c">#include <stdio.h>
#include <string.h>
 
int findNumOfDec(doubleconst xchar *s) {
charint buffer[128]pos = 0;
intwhile (s[pos,] num;&& s[pos++] != '.') {}
return numstrlen(s + pos);
 
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(doubleconst xchar *s) {
int num = findNumOfDec(xs);
printf("%fconst haschar %d*p decimals\n", x,= num) != 1 ? "s" : "";
printf("%s has %d decimal%s\n", s, num, p);
}
 
int main() {
test("12.0");
test("12.3450");
test("12.345555555555345");
test("12.3450345555555555");
test("12.345555555555555555553450");
test(1"12.2345e+5434555555555555555555");
char str[64];
sprintf(bufferstr, "%.14ff", x1.2345e+54);
test(str);
return 0;
}</syntaxhighlight>
{{out}}
<pre>
<pre>12.000000 has 0 decimals
12.345000 has 30 decimals
12.3455560 has 121 decimalsdecimal
12.345000345 has 3 decimals
12.345556345555555555 has 1412 decimals
<pre>12.0000003450 has 04 decimals
1234500000000000060751116919315055127939946206157864960.000000 has 0 decimals</pre>
12.34555555555555555555 has 20 decimals
1234500000000000060751116919315055127939946206157864960.000000 has 06 decimals</pre>
</pre>
 
=={{header|C++}}==
9,485

edits