Commatizing numbers: Difference between revisions

Content added Content deleted
(→‎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: Line 1,163:
=={{header|REXX}}==
=={{header|REXX}}==
The hardest part of the   '''comma'''   function is to locate where a   ''usable''   number starts and ends.
The hardest part of the   '''comma'''   function is to locate where a   ''usable''   number starts and ends.
<lang rexx>/*REXX program add commas (or other chars) to a number within a string (or a char str).*/
<lang rexx>/*REXX program adds commas (or other chars) to a string or a number within a string.*/
@. =
@. =
@.1= "pi=3.14159265358979323846264338327950288419716939937510582097494459231"
@.1= "pi=3.14159265358979323846264338327950288419716939937510582097494459231"
Line 1,185: Line 1,185:
exit /*stick a fork in it, we're all done. */
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
comma: procedure; parse arg _,c,p,t,s /*obtain the number & optional options.*/
comma: procedure; parse arg _,commaChar,period,times /*obtain true case arguments.*/
arg ,cU . /*obtain an uppercase version of C. */
arg ,commaCharUpper /* " uppercase 2nd arg. */
c=word(c ',', 1) /*obtain the commatizing character(s).*/
bla= ' ' /*literal to hold a "blank".*/
if cU=='BLANK' then c=' ' /*special case for a "blank" separator.*/
commaChar= word(commaChar ',', 1) /*define comma (string/char.)*/
o=word(p 3, 1) /*obtain the optional period length. */
if commaCharUpper=='BLANK' then commaChar= bla /*allow the use of 'BLANK'. */
p=abs(o) /*obtain the positive period length. */
every= word(period 3, 1) /*defined "period" to be used*/
t=word(t 999999999, 1) /*obtain max # of "commas" to insert. */
period= abs(every) /*use the absolute value. */
s=word(s 1, 1) /*obtain the optional start position.*/
times= word(times 999999999, 1) /*limits # changes to be made*/
/* [↓] various error tests. */

if \datatype(p, 'W') | \datatype(t, "W") | \datatype(s, 'W') | ,
if \datatype(period, 'W') | , /*test for a whole number. */
t<1 | s<1 | p==0 | arg()>5 then return _ /*any invalid options? */
\datatype(times , 'W') | , /* " " " " " */
period==0 | , /*PERIOD can't be zero. */

n=_'.9'; #=123456789; k=0 /*define some handy-dandy variables. */
arg()>4 then return _ /*More than four arguments? */
/*some argument is invalid. */

if o<0 then do /*using a negative period length ? */
n= _'.9' /*a literal string for end. */
b=verify(_, ' ', , s) /*position of first blank in string. */
digs= 123456789 /*the legal digits for start.*/
e=length(_) - verify( reverse(_), ' ') + 1 - p
/* [↓] note zero is omitted.*/
end
if every<0 then do /*Negative? Treat as chars. */
else do /*using a positive period length. */
beginning= verify(_, bla) /*see if any non-blank chars.*/
b=verify(n, #, 'M', s) /*position of first useable decimal dig*/
if beginning==0 then return _ /*if blanks, return as is. */
z=max(1, verify(n, #'0.', "M", s)) /* " " last " "*/
ending= length(_) - verify( reverse(_), bla) + 1 /*find ending.*/
e=verify(n, #'0', , max(1, verify(n, #"0.", 'M', s) ) ) - p - 1
end /* [↑] find number ending. */
end
else do /*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<t /*commatize the digits.*/
ending= verify(n, digs'0', , verify(n, digs"0.", 'M' ))-period-1
_=insert(c, _, j) /*comma spray ───► #.*/
end /* [↑] find ending of number*/
k= k + 1 /*bump the commatizing.*/
#= 0 /*the count of changes made. */
end /*j*/
if beginning>0 & ending>0 then /* [↓] 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:}}
{{out|output|text=&nbsp; when using the internal default inputs:}}
<pre>
<pre>