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 _,commaChar,period,times /*obtain true case arguments.*/
comma: procedure; parse arg x,sep,period,times,start /*obtain true case arguments.*/
arg ,commaCharUpper /* " uppercase 2nd arg. */
arg ,sepU /* " uppercase 2nd arg. */
bla= ' ' /*literal to hold a "blank".*/
bla= ' ' /*literal to hold a "blank".*/
commaChar= word(commaChar ',', 1) /*define comma (string/char.)*/
sep= word(sep ',', 1) /*define comma (string/char.)*/
if commaCharUpper=='BLANK' then commaChar= bla /*allow the use of 'BLANK'. */
if sepU=='BLANK' then sep= bla /*allow the use of 'BLANK'. */
every= word(period 3, 1) /*defined "period" to be used*/
period= word(period 3, 1) /*defined "period" to be used*/
period= abs(every) /*use the absolute value. */
times= word(times 999999999, 1) /*limits # changes to be made*/
times= word(times 999999999, 1) /*limits # changes to be made*/
start= word(start 1 , 1) /*where to start commatizing.*/
/* [↓] 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') | , /* " " " " " */
period==0 | , /*PERIOD can't be zero. */
\datatype(start , 'W') | , /* " " " " " */
arg()>4 then return _ /*More than four arguments? */
start <1 | , /*start can't be less then 1.*/
/*some argument is invalid. */
arg() >5 then return x /*# of args can't be > 5. */
n= _'.9' /*a literal string for end. */
/* [↑] some arg is invalid. */
digs= 123456789 /*the legal digits for start.*/
op= period /*save the original period. */
/* [↓] note zero is omitted.*/
period= abs(period) /*use the absolute value. */
if every<0 then do /*Negative? Treat as chars. */
n= x'.9' /*a literal string for end. */
beginning= verify(_, bla) /*see if any non-blank chars.*/
digs= 123456789 /*the legal digits for start.*/
if beginning==0 then return _ /*if blanks, return as is. */
digsz= 1234567890 /* " " " " fin. */
ending= length(_) - verify( reverse(_), bla) + 1 /*find ending.*/
digszp= 1234567890. /* " " " " fin. */
end /* [] find number ending. */
/* [] note: no zero in digs*/
else do /*Positive? Treat as numbers*/
if op<0 then do /*Negative? Treat as chars. */
beginning= verify(n, digs, "M") /*find beginning of the num. */
beg= start /*begin at the start. */
ending= verify(n, digs'0', , verify(n, digs"0.", 'M' ))-period-1
L= length9x) /*obtain the length of X. */
end /* [↑] find ending of number*/
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 beginning>0 & ending>0 then /* [↓] process TIMES times*/
if beg>0 & fin>0 then /* [↓] process TIMES times*/
do j=ending to beginning by -period while #<times
do j=fin to beg by -period while #<times
_= insert(commaChar, _, j) /*insert a comma into string.*/
x= insert(sep, x, j) /*insert a comma into string.*/
#= # + 1 /*bump the count of changes. */
#= # + 1 /*bump the count of changes. */
end /*j*/ /*(maybe no changes are made)*/
end /*j*/ /*(maybe no changes are made)*/
return _ /*return the commatized str. */
return x /*return the commatized str. */</lang>
</lang>
{{out|output|text=&nbsp; when using the internal default inputs:}}
{{out|output|text=&nbsp; when using the internal default inputs:}}
<pre>
<pre>