Commatizing numbers: Difference between revisions

m
→‎{{header|REXX}}: corrected a typo, changed indentation for the function, added whitespace.
m (→‎{{header|REXX}}: used a template for the output section.)
m (→‎{{header|REXX}}: corrected a typo, changed indentation for the function, added whitespace.)
Line 800:
 
=={{header|REXX}}==
The hardest part of the   '''comma'''   function is to locate where a useable  ''usable''   number starts and ends.
<lang rexx>/*REXX program add commas (or other chars) to a number within a string (or a char str).*/
@. =
@.1= "pi=3.14159265358979323846264338327950288419716939937510582097494459231"
@.2= "The author has two Z$100000000000000 Zimbabwe notes (100 trillion)."
@.3= "-in Aus$+1411.8millions"
@.4= "===US$0017440 millions=== (in 2000 dollars)"
@.5= "123.e8000 is pretty big."
@.6= "The land area of the earth is 57268900(29% of the surface) square miles."
@.7= "Ain't no numbers in this here words, nohow, no way, Jose."
@.8= "James was never known as 0000000007"
@.9= "Arthur Eddington wrote: I believe there are 15747724136275002577605653961181555468044717914527116709366231425076185631031296 protons in the universe."
@.10= " $-140000±100 millions."
@.11= "6/9/1946 was a good year for some."
 
do i=1 while @.i\==''; if i\==1 then say /*process each string.*/
say 'before:before──►'@.i /*show the before str.*/
if i==1 then say ' after:after──►'comma(@.i, 'blank', 5, , 6) /* p=5, start=6. */
if i==2 then say ' after:after──►'comma(@.i, ".") /*comma=decimal point.*/
if i>2 then say ' after:after──►'comma(@.i) /*use the defaults. */
end /*j*/
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
/*──────────────────────────────────COMMA subroutine────────────────────*/
comma: procedure; parse arg _,c,p,t,s /*getobtain the number and& optional options.*/
arg ,cU . /*getobtain an uppercase version of C. */
c=word(c ',', 1) /*getobtain the commatizing charcharacter(s).*/
if cU=='BLANK' then c=' ' /*special case for a "blank" sepseparator.*/
o=word(p 3, 1) /*getobtain the optional period length. */
p=abs(o) /*getobtain the positive period length. */
t=word(t 999999999, 1) /*getobtain max # of "commas" to insert. */
s=word(s 1, 1) /*getobtain the optional start position. */
 
if \datatype(p, 'W') | \datatype(t, "W") | \datatype(s, 'W') | ,
t<1 | s<1 | p==0 | arg()>5 then return _ /*any invalid options? */
 
n=_'.9'; #=123456789; k=0 /*define some handy-dandy vars. variables. */
 
if o<0 then do /*using a negative period length. ? */
b=verify(_, ' ', , s) /*position of 1stfirst blank in string. */
e=length(_) - verify( reverse(_), ' ') + 1 - p
end
else do /*using a positive period length. */
b=verify(n, #, "'M"', s) /*position of 1stfirst useable digits.decimal dig*/
z=max(1, verify(n, #"'0."', '"M'", s)) /* " " last " "*/
e=verify(n, #'0', , max(1, verify(n, #"0.", 'M', s) ) ) - p - 1
end
 
if e>0 & b>0 then do j=e to b by -p while k<t /*commatize the digsdigits.*/
_=insert(c, _, j) /*comma spray ──► ───► #.*/
k= k + 1 /*bump the commatizing. */
end /*j*/
return _</lang>
{{out|output|text=&nbsp; when using the internal default inputs:}}
<pre>