Commatizing numbers: Difference between revisions
Content added Content deleted
m (added a comment.) |
m (→{{header|REXX}}: tidied up some code, used better variable names..) |
||
Line 1,186: | Line 1,186: | ||
exit /*stick a fork in it, we're all done. */ |
exit /*stick a fork in it, we're all done. */ |
||
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
||
comma: procedure; parse arg |
comma: procedure; parse arg x,sep,period,times,start /*obtain true case arguments.*/ |
||
arg , |
arg ,sepU /* " uppercase 2nd arg. */ |
||
bla= ' ' /*literal to hold a "blank".*/ |
bla= ' ' /*literal to hold a "blank".*/ |
||
sep= word(sep ',', 1) /*define comma (string/char.)*/ |
|||
if |
if sepU=='BLANK' then sep= bla /*allow the use of 'BLANK'. */ |
||
period= word(period 3, 1) /*defined "period" to be used*/ |
|||
⚫ | |||
times= word(times 999999999, 1) /*limits # changes to be made*/ |
times= word(times 999999999, 1) /*limits # changes to be made*/ |
||
⚫ | |||
/* [↓] various error tests. */ |
/* [↓] various error tests. */ |
||
if \datatype(period, 'W') | , /*test for a whole number. */ |
if \datatype(period, 'W') | , /*test for a whole number. */ |
||
\datatype(times , 'W') | , /* " " " " " */ |
\datatype(times , 'W') | , /* " " " " " */ |
||
\datatype(start , 'W') | , /* " " " " " */ |
|||
start <1 | , /*start can't be less then 1.*/ |
|||
arg() >5 then return x /*# of args can't be > 5. */ |
|||
/* [↑] some arg is invalid. */ |
|||
op= period /*save the original period. */ |
|||
period= abs(period) /*use the absolute value. */ |
|||
n= x'.9' /*a literal string for end. */ |
|||
digs= 123456789 /*the legal digits for start.*/ |
|||
digsz= 1234567890 /* " " " " fin. */ |
|||
digszp= 1234567890. /* " " " " fin. */ |
|||
/* [↓] note: no zero in digs*/ |
|||
if op<0 then do /*Negative? Treat as chars. */ |
|||
beg= start /*begin at the start. */ |
|||
L= length9x) /*obtain the length of X. */ |
|||
fin= L - verify( reverse(x), bla) + 1 /*find the ending of the num.*/ |
|||
end /* [↑] find number ending. */ |
|||
else do /*Positive? Treat as numbers*/ |
|||
beg= verify(n, digs, "M",start) /*find beginning of number. */ |
|||
v2=max(verify(n, digszp,'M',start),1) /*end of the usable number. */ |
|||
fin=verify(n, digsz, , v2) -period -1 /*adjust the ending (fin). */ |
|||
end /* [↑] find ending of number*/ |
|||
#= 0 /*the count of changes made. */ |
#= 0 /*the count of changes made. */ |
||
if |
if beg>0 & fin>0 then /* [↓] process TIMES times*/ |
||
do j= |
do j=fin to beg by -period while #<times |
||
x= insert(sep, x, j) /*insert a comma into string.*/ |
|||
#= # + 1 /*bump the count of changes. */ |
|||
end /*j*/ |
end /*j*/ /*(maybe no changes are made)*/ |
||
return |
return x /*return the commatized str. */</lang> |
||
</lang> |
|||
{{out|output|text= when using the internal default inputs:}} |
{{out|output|text= when using the internal default inputs:}} |
||
<pre> |
<pre> |