Commatizing numbers: Difference between revisions

m
→‎{{header|REXX}}: used better variable names, add/changed whitespace and comments.
(→‎top: add `⏨` [U+23E8 “decimal expnonent symbol”] exponent character list)
m (→‎{{header|REXX}}: used better variable names, add/changed whitespace and comments.)
Line 1,163:
=={{header|REXX}}==
The hardest part of the   '''comma'''   function is to locate where a   ''usable''   number starts and ends.
<lang rexx>/*REXX program addadds commas (or other chars) to a number within a string ( or a char str)number within a string.*/
@. =
@.1= "pi=3.14159265358979323846264338327950288419716939937510582097494459231"
Line 1,185:
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
comma: procedure; parse arg _,ccommaChar,pperiod,t,s times /*obtain the number &true optionalcase optionsarguments.*/
arg ,cU . arg ,commaCharUpper /*obtain an uppercase version" of C.uppercase 2nd arg. */
cbla=word(c ', ', 1) /*obtainliteral to thehold a commatizing character(s)"blank".*/
if cU=commaChar= word(commaChar 'BLANK,', 1) then c=' ' /*specialdefine casecomma for a "blank" separator(string/char.)*/
oif commaCharUpper==word(p 3, 1) 'BLANK' then commaChar= bla /*obtainallow the optionaluse periodof length 'BLANK'. */
pevery=abs word(o) period 3, 1) /*obtain the positivedefined "period" length. to be used*/
tperiod=word abs(tevery) 999999999, 1) /*obtain max # of "commas" to insert /*use the absolute value. */
stimes= word(stimes 1999999999, 1) /*obtain thelimits optional# changes startto be position.made*/
/* [↓] various error tests. */
 
if \datatype(pperiod, 'W') | , \datatype(t, "W") | \datatype(s, 'W') | , /*test for a whole number. */
t<1\datatype(times , 'W') | , s<1 | p==0 | arg()>5 then return _ /*any invalid options?" " " " " */
period==0 | , /*PERIOD can't be zero. */
 
n=_'.9'; arg()>4 #=123456789; k=0 then return _ /*define someMore than handy-dandyfour arguments? variables. */
/*some argument is invalid. */
 
ifn= o<0_'.9' then do /*using a negative period length ? /*a literal string for end. */
digs= 123456789 b=verify(_, ' ', , s) /*position of first blank in string. /*the legal digits for start.*/
e=length(_) - verify( reverse(_), ' ') + 1 - p /* [↓] note zero is omitted.*/
if every<0 then do end /*Negative? Treat as chars. */
else do beginning= verify(_, bla) /*usingsee aif any positivenon-blank period lengthchars. */
bif beginning=verify(n,=0 #, 'M', s)then return _ /*positionif blanks, return ofas firstis. useable decimal dig*/
zending=max length(1,_) - verify(n, #'0.'reverse(_), "M", s)bla) + 1 /*find " " last " "ending.*/
e=verify(n,end #'0', , max(1, verify(n, #"0.", 'M', s) ) ) - p - 1 /* [↑] find number ending. */
else enddo /*Positive? Treat as numbers*/
beginning= verify(n, digs, "M") /*find beginning of the num. */
 
if e>0 & b>0 then do j=e to b by -p while k<tending= verify(n, digs'0', , verify(n, digs"0.", /*commatize'M' the digits.*/))-period-1
end _=insert(c, _, j) /*comma spray[↑] find ───►ending of #.number*/
#= 0 k= k + 1 /*the count of changes /*bumpmade. the commatizing.*/
if beginning>0 & ending>0 then end /*j [↓] process TIMES times*/
do j=ending to beginning by -period while #<times
return _</lang>
_= insert(commaChar, _, j) /*insert a comma into string.*/
#= # + 1 /*bump the count of changes. */
end /*j*/ /*(maybe no changes are made)*/
return _ /*return the commatized str. */
</lang>
{{out|output|text=&nbsp; when using the internal default inputs:}}
<pre>