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