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 |
<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; |
comma: procedure; parse arg _,commaChar,period,times /*obtain true case arguments.*/ |
||
arg ,commaCharUpper /* " uppercase 2nd arg. */ |
|||
bla= ' ' /*literal to hold a "blank".*/ |
|||
commaChar= word(commaChar ',', 1) /*define comma (string/char.)*/ |
|||
if commaCharUpper=='BLANK' then commaChar= bla /*allow the use of 'BLANK'. */ |
|||
every= 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*/ |
|||
/* [↓] various error tests. */ |
|||
if \datatype( |
if \datatype(period, 'W') | , /*test for a whole number. */ |
||
\datatype(times , 'W') | , /* " " " " " */ |
|||
period==0 | , /*PERIOD can't be zero. */ |
|||
arg()>4 then return _ /*More than four arguments? */ |
|||
/*some argument is invalid. */ |
|||
n= _'.9' /*a literal string for end. */ |
|||
digs= 123456789 /*the legal digits for start.*/ |
|||
/* [↓] note zero is omitted.*/ |
|||
if every<0 then do /*Negative? Treat as chars. */ |
|||
beginning= verify(_, bla) /*see if any non-blank chars.*/ |
|||
if beginning==0 then return _ /*if blanks, return as is. */ |
|||
ending= length(_) - verify( reverse(_), bla) + 1 /*find ending.*/ |
|||
end /* [↑] find number ending. */ |
|||
else do /*Positive? Treat as numbers*/ |
|||
beginning= verify(n, digs, "M") /*find beginning of the num. */ |
|||
ending= verify(n, digs'0', , verify(n, digs"0.", 'M' ))-period-1 |
|||
end /* [↑] find ending of number*/ |
|||
#= 0 /*the count of changes made. */ |
|||
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= when using the internal default inputs:}} |
{{out|output|text= when using the internal default inputs:}} |
||
<pre> |
<pre> |