Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

formatting a number (with commas)

546 views
Skip to first unread message

Gerard S.

unread,
Nov 30, 1999, 3:00:00 AM11/30/99
to

> Does anyone have any code snipets that will format a number with commas.
> Like so:
>
> 1,000
> 1,203,345
>
> I'm doing this on a mainframe under TSO/ISPF. Any help greatly
appreciated.
>
> Thanks
> tya...@freewwweb.com


tyates: I wrote this subroutine a long while ago --- it started with only
accepting whole integers, without signs, then I supported signs, then numbers
with trailing decimal points, then numbers with fractions, then numbers with
exponents ...

If you want to, you could trim the parts that you don't need, the code (me
thinks) can be easily trimmed for that purpose.

I had inteded to make the whole thing very pretty to read, but with my newsgroup
editor, it wants to use proportional characters, so the neat boxed look like ...
well, crap.

Anyway, I hope ya can use the subroutine.

By the way, I also have subroutines to transform a (whole) number into it's
ordinal number, i.e.:

1 --> 1st
2 --> 2nd
3 --> 3rd
4 --> 4th
110 --> 110th
0 --> 0th
11 --> 11th
21 --> 21st
etc

Also, I have a routine to spell out a number in English (with or without commas
added), and it also supports writing 1999 (as an option) as
nineteen hundred ninety nine (instead of one thousand nine hundred ninety
nine).

Also, another REXX routine to convert (whole, non-negative) numbers --> Roman
(three types), and Roman --> (Arabic) nurbers.

Hope I don't sound like a commercial.

By the way, if the REXX built-in function VERIFY available on all REXX
compilers/interpreters (espeically the MATCH option)?


Gerard S.


/*+--------------------------------------------------------------------+
|REXX subroutine to insert comma(s) into any type of REXX numbers, |
|including: signed/unsigned integers, numbers with fractions, and |
|numbers in exponential notation (with an "e" or "E" exponent). |
| |
|If the arguement isn't a valid number, it is passed back unchanged |
|with no error message (this can be readily changed, nowever). |
| |
|Any superflous leading zeros, or trailing zeros are left intact. |
| |
|Coding notes: |
| |
|The use of the character "^" is the logical not symbol. In |
|actuality, the 'AA'x character is used (a true "not" symbol, but it |
|isn't supported by all REXX interpreters). |
| |
|Normally, this subroutine is a one liner, that is, all statements |
|are written on one line, seperated by semicolons of course, and |
|there are no comments. I've rewritten this code to break up the |
|"one-line" and added numerous comments. |
| |
|I tend to use subroutines without the PROCEDURE clause for reasons |
|a bit complicated to get into at this time. |
| |
|I use the underscore (_) to indicate temporary variables, both for |
|immedicate recognition of their temporary existence, and brevity. |
| |
|The "if _j==1 then if ^datatype(left ..." statement is broken |
|up into a compound statement instead of a compound IF statement |
|for performance reasons, at the expense of readability. |
+--------------------------------------------------------------------+*/


comma: _=arg(1) /*get a shorter version of the arg. */
if ^datatype(_,'N') then return _ /*if not numeric, then return as is.*/
_t=length(_) /*get a temp var, the arg's length. */
_f=verify(_,'.eE',"M") /*find fraction part or exponent? */
if _f^==0 then _t=_f-1 /*if so, limit up to dot or exponent*/

do _j=_t-3 by -3 to 1 /*step through the integer part of #*/
if _j==1 then if ^datatype(left(_,1),'W') then leave /*ignore sign.*/
_=insert(",",_,_j) /*insert a comma (,) appropriately. */
end

return _ /*return the now commatized nuber. */

Walt Smith

unread,
Dec 1, 1999, 3:00:00 AM12/1/99
to
In article <EOW04.809$%z6....@newsfeed.slurp.net>,
"Gerard S." <ger...@prairietech.net> wrote:

Tips for sharing code:

1) If using Deja.com, click on "View original Usenet format" at the
lower left of the message area, you'll see the code in a fixed
non-proportional font. Copy & paste into your favorite editor, ship it
off to whatever platform you want from there.

2) Using remarq.com: they support HTML tags in the postings so always
post code using the PRE and /PRE tags (each tag is enclosed in < and >
symbols). To retrieve it, just copy and paste into your PC editor & off
you go.

3) Long "snippets" are best shared via e-mail, though not everyone gets
to see & learn then. You can also do a double barreled approach - post
it for all and e-mail directly to the person.

----------------------------------------------------------------------
Walt Smith | All models are wrong
wjsm...@fedex.com | Some models are useful


Sent via Deja.com http://www.deja.com/
Before you buy.

0 new messages