Getting the number of decimal places: Difference between revisions

m
→‎{{header|Phix}}: use pygments
(→‎{{header|C}}: {{Template:Incorrect}})
m (→‎{{header|Phix}}: use pygments)
(14 intermediate revisions by 8 users not shown)
Line 1:
{{draft task}}
;Task:
Write a program (function) to get the number of decimalsdecimal places in a given number.
 
 
Line 113:
1E-10 has 10 decimals
1.23456789E+10 has 0 decimals
</pre>
 
=={{header|Ada}}==
 
<syntaxhighlight lang="ada">
-- Report the number of decimal places in a number
-- J. Carter 2023 Apr
-- I presume the input is a String containing the image of the value; the test values of 12.345 & 12.3450 represent the same number,
-- and so would give the same result otherwise
 
with Ada.Strings.Fixed;
with Ada.Text_IO;
 
procedure Decimal_Places is
function Num_Places (Number : in String) return Natural;
-- Returns the number of decimal places in the numeric image in Number
function Num_Places (Number : in String) return Natural is
Dot : constant Natural := Ada.Strings.Fixed.Index (Number, ".");
begin -- Num_Places
if Dot = 0 then
return 0;
end if;
return Number'Last - Dot;
end Num_Places;
Test_1 : constant String := "12.345";
Test_2 : constant String := "12.3450";
begin -- Decimal_Places
Ada.Text_IO.Put_Line (Item => Test_1 & (1 .. 10 - Test_1'Length => ' ') & Num_Places (Test_1)'Image);
Ada.Text_IO.Put_Line (Item => Test_2 & (1 .. 10 - Test_2'Length => ' ') & Num_Places (Test_2)'Image);
end Decimal_Places;
</syntaxhighlight>
 
{{out}}
<pre>
12.345 3
12.3450 4
</pre>
 
Line 167 ⟶ 206:
 
=={{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 strlen(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(str, "%f", 1.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
12.3450 has 4 decimals
1234500000000000060751116919315055127939946206157864960.000000 has 0 decimals</pre>
12.34555555555555555555 has 20 decimals
1234500000000000060751116919315055127939946206157864960.000000 has 6 decimals
</pre>
 
=={{header|C++}}==
{{trans|C}}
<syntaxhighlight lang="cpp">#include <iomanipiostream>
#include <iostreamcstring>
#include <sstream>
 
int findNumOfDec(double x) {
std::stringstream ss;
ss << std::fixed << std::setprecision(14) << x;
 
auto s = ss.str();
auto pos = s.find('.');
if (pos == std::string::npos) {
return 0;
}
 
auto tail = s.find_last_not_of('0');
 
int findNumOfDec(const char *s) {
return tail - pos;
int pos = 0;
while (s[pos] && s[pos++] != '.') {}
return strlen(s + pos);
}
 
void test(doubleconst xchar *s) {
std::coutint <<num x << " has " <<= findNumOfDec(xs) << " decimals\n";
const char *p = num != 1 ? "s" : "";
std::cout << s << " has " << num << " decimal" << p << "\n";
}
 
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(str, "%f", 1.2345e+54);
test(str);
return 0;
}</syntaxhighlight>
{{out}}
<pre>12 has 0 decimals
12 has 0 decimals
12.0 has 1 decimal
12.345 has 3 decimals
12.3456345555555555 has 12 decimals
12.3453450 has 34 decimals
12.345634555555555555555555 has 1420 decimals
1234500000000000060751116919315055127939946206157864960.000000 has 6 decimals
1.2345e+54 has 0 decimals</pre>
</pre>
 
=={{header|EasyLang}}==
<syntaxhighlight>
func ndec n .
while abs (n - floor (n + 1e-15)) > 1e-15
n *= 10
r += 1
.
return r
.
for i in [ 0.00000000000001 12.345 12.3450 1.1 0.1234567 ]
write ndec i & " "
.
</syntaxhighlight>
{{out}}
<pre>
14 3 3 1 7
</pre>
 
=={{header|F_Sharp|F#}}==
<syntaxhighlight lang="fsharp">
//Getting the number of decimal places. Nigel Galloway: March 23rd., 2023.
let fN g=let n,g=Seq.length g,g|>Seq.tryFindIndex((=)'.') in match g with Some g->n-g-1 |_->0
["12";"12.00";"12.345";"12.3450";"12.34500"]|>List.iter(fN>>printfn "%d")
</syntaxhighlight>
{{out}}
<pre>
0
2
3
4
5
</pre>
 
=={{header|FreeBASIC}}==
Line 281 ⟶ 335:
12.345677 has 6 decimals
0.142857142857142 has 15 decimals</pre>
 
 
=={{header|Go}}==
Line 370 ⟶ 423:
<pre>[1,3,3,12,15,7]</pre>
=={{header|Java}}==
<syntaxhighlight lang="java">
int decimalPlaces(double value) {
String string = String.valueOf(value);
return string.length() - (string.indexOf('.') + 1);
}
</syntaxhighlight>
Or
<syntaxhighlight lang="java">public static int findNumOfDec(double x){
String str = String.valueOf(x);
Line 568 ⟶ 628:
 
=={{header|Phix}}==
<!--<syntaxhighlight lang="phix">(phixonline)-->
<syntaxhighlight lang="phix">
<span style="color: #008080;">constant</span> <span style="color: #000000;">fracfmt</span> <span style="color: #0000FF;">=</span> <span style="color: #008080;">iff</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">machine_bits</span><span style="color: #0000FF;">()=</span><span style="color: #000000;">32</span><span style="color: #0000FF;">?</span><span style="color: #008000;">"%.14g"</span><span style="color: #0000FF;">:</span><span style="color: #008000;">"%.18g"</span><span style="color: #0000FF;">)</span>
constant fracfmt = iff(machine_bits()=32?"%.14g":"%.18g")
<span style="color: #008080;">function</span> <span style="color: #000000;">num_decimals</span><span style="color: #0000FF;">(</span><span style="color: #004080;">object</span> <span style="color: #000000;">o</span><span style="color: #0000FF;">)</span>
function num_decimals(object o)
<span style="color: #004080;">integer</span> <span style="color: #000000;">nd</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">-</span><span style="color: #000000;">1</span>
integer nd = -1
<span style="color: #004080;">string</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">t</span>
string s, t
<span style="color: #008080;">if</span> <span style="color: #004080;">integer</span><span style="color: #0000FF;">(</span><span style="color: #000000;">o</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">then</span>
if integer(o) then
<span style="color: #000000;">nd</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span>
nd = 0
<span style="color: #000000;">s</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">sprintf</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"%d"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">o</span><span style="color: #0000FF;">)</span>
s = sprintf("%d",o)
<span style="color: #008080;">elsif</span> <span style="color: #004080;">atom</span><span style="color: #0000FF;">(</span><span style="color: #000000;">o</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">then</span>
elsif atom(o) then
<span style="color: #000000;">s</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">sprintf</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"%.19g"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">o</span><span style="color: #0000FF;">)</span>
s = sprintf("%.19g",o)
<span style="color: #000000;">o</span> <span style="color: #0000FF;">-=</span> <span style="color: #7060A8;">trunc</span><span style="color: #0000FF;">(</span><span style="color: #000000;">o</span><span style="color: #0000FF;">)</span>
o -= trunc(o)
<span style="color: #008080;">if</span> <span style="color: #000000;">o</span><span style="color: #0000FF;">=</span><span style="color: #000000;">0</span> <span style="color: #008080;">then</span>
if o=0 then
<span style="color: #000000;">nd</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span>
<span style nd ="color: #008080;">else</span>0
else
<span style="color: #000000;">t</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">sprintf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">fracfmt</span><span style="color: #0000FF;">,</span><span style="color: #000000;">o</span><span style="color: #0000FF;">)</span>
t = sprintf(fracfmt,o)
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
end if
<span style="color: #008080;">elsif</span> <span style="color: #004080;">string</span><span style="color: #0000FF;">(</span><span style="color: #000000;">o</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">then</span>
elsif string(o) then
<span style="color: #000000;">s</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">o</span>
s = o
<span style="color: #000000;">t</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">s</span>
t = s
<span style="color: #008080;">else</span>
else
<span style="color: #7060A8;">crash</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"unknown type"</span><span style="color: #0000FF;">)</span>
crash("unknown type")
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
end if
<span style="color: #008080;">if</span> <span style="color: #000000;">nd</span><span style="color: #0000FF;">=-</span><span style="color: #000000;">1</span> <span style="color: #008080;">then</span>
if nd=-1 then
<span style="color: #004080;">integer</span> <span style="color: #000000;">e</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">find</span><span style="color: #0000FF;">(</span><span style="color: #008000;">'e'</span><span style="color: #0000FF;">,</span><span style="color: #000000;">t</span><span style="color: #0000FF;">)</span>
integer e = find('e',t)
<span style="color: #008080;">if</span> <span style="color: #000000;">e</span> <span style="color: #008080;">then</span>
if e then
<span style="color: #0000FF;">{</span><span style="color: #000000;">t</span><span style="color: #0000FF;">,</span><span style="color: #000000;">e</span><span style="color: #0000FF;">}</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">t</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">..</span><span style="color: #000000;">e</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">],</span><span style="color: #7060A8;">to_number</span><span style="color: #0000FF;">(</span><span style="color: #000000;">t</span><span style="color: #0000FF;">[</span><span style="color: #000000;">e</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">..$])}</span>
{t,e} = {t[1..e-1],to_number(t[e+1..$])}
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
end if
<span style="color: #004080;">integer</span> <span style="color: #000000;">dot</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">find</span><span style="color: #0000FF;">(</span><span style="color: #008000;">'.'</span><span style="color: #0000FF;">,</span><span style="color: #000000;">t</span><span style="color: #0000FF;">)</span>
integer dot = find('.',t)
<span style="color: #000000;">nd</span> <span style="color: #0000FF;">=</span> <span style="color: #008080;">iff</span><span style="color: #0000FF;">(</span><span style="color: #000000;">dot</span><span style="color: #0000FF;">?</span><span style="color: #7060A8;">max</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">t</span><span style="color: #0000FF;">)-</span><span style="color: #000000;">dot</span><span style="color: #0000FF;">-</span><span style="color: #000000;">e</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0</span><span style="color: #0000FF;">):</span><span style="color: #7060A8;">max</span><span style="color: #0000FF;">(-</span><span style="color: #000000;">e</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0</span><span style="color: #0000FF;">))</span>
nd = iff(dot?max(length(t)-dot-e,0):max(-e,0))
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
end if
<span style="color: #000000;">s</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">shorten</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"digits"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">5</span><span style="color: #0000FF;">)</span>
s = shorten(s,"digits",5)
<span style="color: #008080;">return</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">s</span><span style="color: #0000FF;">,</span><span style="color: #000000;">nd</span><span style="color: #0000FF;">}</span>
return {s,nd}
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
end function
<span style="color: #004080;">sequence</span> <span style="color: #000000;">tests</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #008000;">"0.00100"</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">0.00100</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">001.805</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">1</span><span style="color: #0000FF;">/</span><span style="color: #000000;">3</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">12</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">12.345</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">12.345555555555</span><span style="color: #0000FF;">,</span>
sequence tests = {"0.00100", 0.00100, 001.805, 1/3, 12, 12.345, 12.345555555555,
<span style="color: #008000;">"12.3450"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"12.34555555555555555555"</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">12.345e53</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">1.2345e-08</span><span style="color: #0000FF;">,</span>
"12.3450", "12.34555555555555555555", 12.345e53, 1.2345e-08,
<span style="color: #008000;">"12.345e53"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"1.2345e-08"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"0.1234567890987654321"</span><span style="color: #0000FF;">,</span>
"12.345e53", "1.2345e-08", "0.1234567890987654321",
<span style="color: #008000;">"124093581919.648947697827373650380188008224280338254175148904323577880859375"</span><span style="color: #0000FF;">}</span>
"124093581919.648947697827373650380188008224280338254175148904323577880859375"}
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">tests</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
for i=1 to length(tests) do
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%25s has %d decimals\n"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">num_decimals</span><span style="color: #0000FF;">(</span><span style="color: #000000;">tests</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]))</span>
printf(1,"%25s has %d decimals\n",num_decimals(tests[i]))
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
end for
<!--</syntaxhighlight>-->
</syntaxhighlight>
{{out}}
32 bit
Line 883 ⟶ 944:
<pre>
4
</pre>
 
'''Another version'''
<syntaxhighlight lang="ring">
decimals(4)
num = 5.1945
strnum = string(num)
pos = substr(strnum,".")
dec = len(strnum) - pos
see "Number of decimals: " + dec + nl
</syntaxhighlight>
{{out}}
<pre>
Number of decimals: 4
</pre>
 
=={{header|RPL}}==
≪ DUP MANT →STR SIZE SWAP XPON - 2 - 0 MAX
≫ ''''NDEC'''' STO
≪ { 12 120 12.345 12.345677 1.23E-20 1.23E20 } → cases
≪ { } 1 cases SIZE '''FOR''' j
cases j GET '''NDEC''' + '''NEXT'''
≫ ≫ ''''TASK'''' STO
{{out}}
<pre>
1: { 0 0 3 6 22 0 }
</pre>
 
Line 933 ⟶ 1,021:
 
Converting the fourth example to a Rat or BigRat object wouldn't help as the constructor for those classes automatically reduces the numerator and denominator to their lowest terms. BigRat would work for the fifth example but the argument would have to be passed as a string anyway so we might as well just parse the string.
<syntaxhighlight lang="ecmascriptwren">var error = "Argument must be a number or a decimal numeric string."
 
var getNumDecimals = Fn.new { |n|
7,815

edits