Getting the number of decimal places: Difference between revisions
Content added Content deleted
Alextretyak (talk | contribs) (Added 11l) |
(Added solution for Action!) |
||
Line 25: | Line 25: | ||
3 |
3 |
||
4 |
4 |
||
</pre> |
|||
=={{header|Action!}}== |
|||
{{libheader|Action! Tool Kit}} |
|||
<lang Action!>INCLUDE "D2:REAL.ACT" ;from the Action! Tool Kit |
|||
BYTE FUNC FindC(CHAR ARRAY s CHAR c) |
|||
BYTE i |
|||
FOR i=1 TO s(0) |
|||
DO |
|||
IF s(i)=c THEN |
|||
RETURN (i) |
|||
FI |
|||
OD |
|||
RETURN (0) |
|||
BYTE FUNC DecimalCount(REAL POINTER r) |
|||
CHAR ARRAY s(20),sub(20) |
|||
BYTE i,dotPos,ePos,count |
|||
INT exp |
|||
StrR(r,s) |
|||
ePos=FindC(s,'E) |
|||
IF ePos>0 THEN |
|||
ePos==+1 |
|||
IF s(ePos)='+ THEN |
|||
ePos==+1 |
|||
FI |
|||
SCopyS(sub,s,ePos,s(0)) |
|||
exp=ValI(sub) |
|||
ELSE |
|||
exp=0 |
|||
FI |
|||
dotPos=FindC(s,'.) |
|||
count=0 |
|||
IF dotPos>0 THEN |
|||
FOR i=dotPos+1 TO s(0) |
|||
DO |
|||
IF s(i)<'0 OR s(i)>'9 THEN |
|||
EXIT |
|||
FI |
|||
count==+1 |
|||
OD |
|||
FI |
|||
IF exp<0 THEN |
|||
count==-exp |
|||
ELSEIF exp<count THEN |
|||
count==-exp |
|||
ELSE |
|||
count=0 |
|||
FI |
|||
RETURN (count) |
|||
PROC Test(REAL POINTER r) |
|||
BYTE count |
|||
count=DecimalCount(r) |
|||
PrintR(r) |
|||
PrintF(" has %I decimals%E",count) |
|||
RETURN |
|||
PROC Main() |
|||
REAL r |
|||
Put(125) PutE() ;clear screen |
|||
ValR("1234",r) Test(r) |
|||
ValR("123.4",r) Test(r) |
|||
ValR("12.34",r) Test(r) |
|||
ValR("1.234",r) Test(r) |
|||
ValR("0.1234",r) Test(r) |
|||
ValR("1.234E-3",r) Test(r) |
|||
ValR("1.234E-10",r) Test(r) |
|||
ValR("1.E-10",r) Test(r) |
|||
ValR("1.23456789E10",r) Test(r) |
|||
RETURN</lang> |
|||
{{out}} |
|||
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Getting_the_number_of_decimals.png Screenshot from Atari 8-bit computer] |
|||
<pre> |
|||
1234 has 0 decimals |
|||
123.4 has 1 decimals |
|||
12.34 has 2 decimals |
|||
1.234 has 3 decimals |
|||
.1234 has 4 decimals |
|||
1.234E-03 has 6 decimals |
|||
1.234E-10 has 13 decimals |
|||
1E-10 has 10 decimals |
|||
1.23456789E+10 has 0 decimals |
|||
</pre> |
</pre> |
||