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

Way to Format numbers with commas

427 views
Skip to first unread message

Jayprakash Narayan

unread,
May 2, 2002, 7:45:56 PM5/2/02
to
Hi,
Once you format the no, with the desired commas, it is no longer a no
from the program point of view. It has to be processed as string. I don't
know you would like to do this as getting the numeric value back from the
formatted string needs another subroutine.

Cheers,
Jay

Guy LHeureux wrote:

> Does anyone know of an easy way to format numbers into numbers with the
> commas in the correct location, ie 123456789 becomes 123,456,789.
>
> Thanks
>
> ----------------------------------------------------------------------
> For TSO-REXX subscribe / signoff / archive access instructions,
> send email to LIST...@VM.MARIST.EDU with the message: INFO TSO-REXX

----------------------------------------------------------------------
For TSO-REXX subscribe / signoff / archive access instructions,
send email to LIST...@VM.MARIST.EDU with the message: INFO TSO-REXX

Dave Petronella

unread,
May 2, 2002, 7:48:02 PM5/2/02
to
This should work for you:


RECVAL = STRIP(SPACE( ,
TRANSLATE("ABC,DEF,GHI,JKL", ,
RIGHT(RECVAL,12), "ABCDEFGHIJKL"), 0) , , ",")

where RECVAL is a variable name of your choice and holds the number without
the commas before the statement is executed and will have the commas after
the statement is executed.

In a message dated 05/02/2002 7:38:00 PM Eastern Daylight Time,

Guy LHeureux

unread,
May 2, 2002, 8:20:36 PM5/2/02
to
Does anyone know of an easy way to format numbers into numbers with the
commas in the correct location, ie 123456789 becomes 123,456,789.

Thanks

----------------------------------------------------------------------

Robert Zenuk

unread,
May 2, 2002, 8:55:39 PM5/2/02
to
>RECVAL = STRIP(SPACE( ,
>TRANSLATE("ABC,DEF,GHI,JKL", ,
>RIGHT(RECVAL,12), "ABCDEFGHIJKL"), 0) , , ",")

I guess I'm just too dense to figure out how to make this work for any number (or it is getting late)...

Here is another little routine to format any number with commas (it also handles numbers with decimal places).

/* rexx - commafy */
arg num
revnum = reverse(num)
if pos('.',revnum) <> 0 then
parse var revnum mantissa '.' revnum
else
mantissa = ''
do while start < length(revnum) - 4
start = lastpos(',',revnum) + 3
revnum = insert(',',revnum,start)
end
num = reverse(revnum)
if mantissa <> '' then num = num || '.' || reverse(mantissa)
say num

I think there was also something (a little more elegant than mine) about this a few months ago on the list. Take a look in the archives.

HTH,

Rob

GerardS

unread,
May 2, 2002, 10:43:56 PM5/2/02
to
| Guy LHeureux asked:

| Does anyone know of an easy way to format numbers into numbers with the
| commas in the correct location, ie 123456789 becomes 123,456,789.

Well, it's easy, 'cause the routine is already written. It will commatize
any number, no restrictions on DIGITS(), handle signed numbers, numbers
with decimal points and/or fractions, as well as exponentated numbers. It
also can support "European" style numbers with periods (or any other
characters) for "commas". Example of use: newnum = comma(num)
___________________________________________________________________________
comma: procedure; parse arg _,c; if c=='' then c=","; n=_'.9'
do j=verify(n,1234567890,,verify(n,1234567890.,'M'))-4 to ,
verify(n,987654321,"M") by -3;_=insert(c,_,j)
end
return _
__________________________________________________________________Gerard S.

Karlheinz Wittemann

unread,
May 3, 2002, 5:03:46 AM5/3/02
to
Hi,

i wrote something like this for a friend. the program works in both directions but with german rules (decimalpoint is comma)

in: 123456 # out: 123.456
in: 12345,67 # out 1.2345,67
usage: print_formatted = pu§fmt$e("12345,67","D")

or

in: 12.345.678 # out: 12345678
in: 12.345,78 # out: 12345,78
usage: computational = pu§fmt$e("12.345,67","R")

i stay here only with my windows-pc without rexx and cannon test. so, all what you have to do is to change decimalpoint from "," to "." and the thousand-point from "." to ","

have fun
Heinz
/* PU§FMT$E **********************************************************
* *
* Formatierung von Zahlen. *
* Parameter 1. zu formatierende Zahl *
* 2. Formatierungsart *
* D = Druckaufbereitet *
* R = Rechenf„hig *
* *
* Erstellt: 29.06.1999 Wittemann *
* *
*********************************************************************/

H_PROG_START:

parse source . TYP .
if TYP = "FUNCTION" then arg WERT,ART,DMY
else arg WERT ART DMY
WERT = strip(WERT)
if ART <> "R" then ART = "D"
select
when ART = "R" then call format_rechenfaehig
when ART = "D" then call format_druckaufbereitet
otherwise nop
end

H_PROG_START:

return NEW


format_rechenfaehig:

/******************************************************************
* Druckaufbereitungspunkte entfernen, Dezimal "." statt "," *
******************************************************************/
parse var WERT INTEGER "," DECIMAL
NEW = space(translate(INTEGER," ","."),0)
if DECIMAL <> "" then NEW = NEW"."DECIMAL
return NEW


format_druckaufbereitet:

/******************************************************************
* Tausender "." setzen, Dezimal "," statt "." *
******************************************************************/
parse var WERT INTEGER "." DECIMAL
INTEGER = reverse(INTEGER)
NEW=""
do I = 1 to length(INTEGER)
NEW = NEW !! substr(INTEGER,I,1)
if I // 3 = 0 then do
NEW = NEW"."
end
end
NEW = reverse(NEW)
if left(NEW,1) = "." then NEW = substr(NEW,2)
if DECIMAL <> "" then NEW = NEW","DECIMAL
return NEW

TSO REXX Discussion List <TSO-...@VM.MARIST.EDU> schrieb am 03.05.02:


> Does anyone know of an easy way to format numbers into numbers with the
> commas in the correct location, ie 123456789 becomes 123,456,789.
>

> Thanks


>
> ----------------------------------------------------------------------
> For TSO-REXX subscribe / signoff / archive access instructions,
> send email to LIST...@VM.MARIST.EDU with the message: INFO TSO-REXX


______________________________________________________________________________
All inclusive! 100 MB Speicher, werbefrei, SMS günstiger, Wunschrufnummer, Events,
Preisvorteile und mehr unter http://club.web.de/?mc=021104

Ryerse, Robin

unread,
May 3, 2002, 8:23:44 AM5/3/02
to
The real frustration with this challenge is knowing that (in OS/390) there
is a single machine instruction (EDMK) that does the job precisely.

GerardS

unread,
May 3, 2002, 12:17:30 PM5/3/02
to
| Ryerse, Robin wrote:
| The real frustration with this challenge is knowing that (in OS/390) there
| is a single machine instruction (EDMK) that does the job precisely.
|
|> Guy LHeureux asked:

|> Does anyone know of an easy way to format numbers into numbers with the
|> commas in the correct location, ie 123456789 becomes 123,456,789.

EDMK will only handle integers, not numbers in general. It also has a length
restriction, and can't handle exponentated numbers and other such oddities.
________________________________________________________________Gerard S.

Frank Yaeger

unread,
May 3, 2002, 2:33:39 PM5/3/02
to
Guy LHeureux wrote:

> Does anyone know of an easy way to format numbers into numbers with the
> commas in the correct location, ie 123456789 becomes 123,456,789.

DFSORT's INREC, OUTREC and OUTFIL OUTREC statements allow you to use 27
pre-defined edit masks or your own user defined edit masks for this kind of
thing. For example, p,9,ZD,EDIT=(TTT,TTT,TTT) if you want to print leading
zeros or p,9,ZD,EDIT=(III,III,IIT) if you don't want to suppress leading
zeros or M12 if you want a sign and suppressed leading zeros. Lots of
variations are possible. If you want more help using DFSORT for this, let
me know exactly what your input values look like and what you want your
output values to look like in terms of signs, leading zeros, etc.

--
Frank Yaeger - DFSORT Team
Specialties: ICETOOL, OUTFIL, Symbols, Migration
=> DFSORT/MVS is on the WWW at http://www.ibm.com/storage/dfsort/


Frank Yaeger

unread,
May 3, 2002, 2:35:25 PM5/3/02
to
Frank Yaeger wrote:

> ... p,9,ZD,EDIT=(III,III,IIT) if you don't want to suppress leading
> zeros ...

Sorry, that should be "if you want to suppress leading zeros".

Gary Weinhold

unread,
May 3, 2002, 2:38:24 PM5/3/02
to
Well, technically, it handled packed decimal, placing a decimal point
anywhere you wanted it. I always thought of it as a pretty complex
instruction for when it was introduced (later, of course, Program Call
came along to beat all previous contenders for complexity).

Gary Weinhold
Data Kinetics, Ltd

Tom Parker

unread,
May 3, 2002, 2:42:25 PM5/3/02
to
Have you heard about the 64-bit instructions, like Store Multiple Disjoint?
Store 32 bits of each resigter in the usual contiguous words, store the
OTHER 32 bits in another set of contiguous words somewhere else entirely,
so I hear.


Gary Weinhold
<weinhold To: TSO-...@VM.MARIST.EDU
@DKL.COM> cc:
Sent by: TSO Subject: Re: Way to Format numbers with commas
REXX
Discussion
List
<TSO-REXX


05/03/02
01:19 PM
Please
respond to
TSO REXX
Discussion
List

0 new messages