Jump to content

Commatizing numbers: Difference between revisions

m
→‎{{header|REXX}}: tidied up some code, used better variable names..
m (added a comment.)
m (→‎{{header|REXX}}: tidied up some code, used better variable names..)
Line 1,186:
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
comma: procedure; parse arg _x,commaCharsep,period,times,start /*obtain true case arguments.*/
arg ,commaCharUppersepU /* " uppercase 2nd arg. */
bla= ' ' /*literal to hold a "blank".*/
commaCharsep= word(commaCharsep ',', 1) /*define comma (string/char.)*/
if commaCharUppersepU=='BLANK' then commaCharsep= bla /*allow the use of 'BLANK'. */
everyperiod= 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*/
periodstart= absword(every) start 1 , 1) /*usewhere theto absolutestart valuecommatizing. */
/* [↓] various error tests. */
if \datatype(period, 'W') | , /*test for a whole number. */
\datatype(times , 'W') | , /* " " " " " */
period==0\datatype(start , 'W') | , | , /* " " " " /*PERIOD can't be" zero. */
arg()>4start <1 | , then return _ /*Morestart thancan't fourbe arguments?less then 1.*/
arg() >5 then return x /*# of args can't be > /*some argument is invalid5. */
n= _'.9' /*a literal[↑] stringsome forarg is endinvalid. */
digsop= 123456789period /*save the legaloriginal digitsperiod. for start.*/
period= abs(period) /*use [↓]the absolute value. note zero is omitted.*/
ifn= x'.9' every<0 then do /*Negative?a literal string Treatfor as charsend. */
digs= 123456789 beginning= verify(_, bla) /*seethe iflegal anydigits non-blankfor charsstart.*/
digsz= 1234567890 if beginning==0 then return _ /*if blanks," return as is. " " " fin. */
digszp= 1234567890. ending= length(_) - verify( reverse(_), bla) + 1 /*find ending" " " " fin. */
end /* [] findnote: numberno ending.zero in digs*/
if op<0 then do else do /*PositiveNegative? Treat as numberschars. */
beg= start beginning= verify(n, digs, "M") /*findbegin beginning ofat the numstart. */
L= length9x) ending= verify(n, digs'0', , verify(n, digs"0 /*obtain the length of X.", 'M' ))-period-1 */
fin= L - end verify( reverse(x), bla) + 1 /*find [↑] findthe ending of numberthe 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. */
if beginningbeg>0 & endingfin>0 then /* [↓] process TIMES times*/
do j=endingfin to beginningbeg by -period while #<times
_ x= insert(commaCharsep, _x, j) /*insert a comma into string.*/
#= # + 1 #= # + 1 /*bump the count of changes. */
end /*j*/ /*(maybe no changes are made)*/
return _x /*return the commatized str. */</lang>
</lang>
{{out|output|text=&nbsp; when using the internal default inputs:}}
<pre>
Cookies help us deliver our services. By using our services, you agree to our use of cookies.