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

Need Rexx function to get numbers rounded

1,990 views
Skip to first unread message

George Shedlock

unread,
Sep 28, 1993, 6:39:44 PM9/28/93
to
How can you set up a rexx function such that the result is rounded to
the next lower whole number?

Albert Crosby

unread,
Sep 28, 1993, 8:42:54 PM9/28/93
to
George Shedlock <TEC...@TSCVM.BITNET> writes:

>How can you set up a rexx function such that the result is rounded to
>the next lower whole number?

Here's one floor function... I'm sure there are better ones...

/* A fairly simple floor function in REXX */

/* Note: this procedure checks for text passed by mistake, and handles numbers
in scientific notation. It returns a value in scientific notation depending
on the current options of REXX. */

floor: procedure
parse arg num
if datatype(num,'N')=0
then return "" /* Return empty string if non-numeric */
num=format(num,,,0) /* Expand scientific format numbers */
period=pos('.',num) /* Find where the period is */
if period=0 /* No period, so... */
then return format(num) /* Return num in scientific format */
value=left(num,period-1)) /* Get the whole number portion */
if value<0 then value=value-1 /* Adjust for negative numbers */
return format(value) /* Return the floor in scientific format */

--
Albert Crosby | Microcomputer & Network Support | WANTED: any good
acr...@uafhp.uark.edu | University of Arkansas | "Intro To The Net
or AL.CROSBY on GENIE | College of Agriculture And | For Newbies" guides
1 501 575 4452 | Home Economics | (email for mine...)

Gary Hasul

unread,
Sep 28, 1993, 11:30:00 PM9/28/93
to
TE>LIST%9309281...@UGA.CC.UGA.EDU>
TE>Newsgroup: comp.lang.rexx

TE>How can you set up a rexx function such that the result is rounded to
TE>the next lower whole number?

try using the TRUNC function
func:
arg x, y, z
/* your code here */
return trunc(your_result)
---
ş SLMR 2.1a ş

W. J. Metzger

unread,
Sep 28, 1993, 5:02:48 PM9/28/93
to
On Tue, 28 Sep 1993 17:39:44 EST George Shedlock said:
>How can you set up a rexx function such that the result is rounded to
>the next lower whole number?

Use the % operator:
x%1
is x rounded down

Roger Deschner

unread,
Sep 29, 1993, 8:29:21 AM9/29/93
to
*BEWARE* of exactly what you mean by "round". Strictly speaking:

"Round" means to change a number to the nearest integer. That is, 0.6
is rounded to 1, and 0.4 is rounded to 0. (Exactly .5 rounds up.)
"Truncate" means to change a number to the integer below. That is, 0.4
is truncated to 0, and 0.9 is also truncated to 0.

To "Round" as per the above definition: x = TRUNC(x+0.5)
To "Truncate" as per the above definition: x = TRUNC(x)

I prefer the TRUNC function over the % operator beause it is clearer
what your code is doing. TRUNC also has a nice option for specifying
how many digits to truncate to.

Horst Kiehl

unread,
Sep 30, 1993, 5:14:18 AM9/30/93
to
In article <28alme$o...@uafhp.uark.edu>,
acr...@uafhp.uark.edu (Albert Crosby) writes:

>period=pos('.',num) /* Find where the period is */
>if period=0 /* No period, so... */
> then return format(num) /* Return num in scientific format */
>value=left(num,period-1)) /* Get the whole number portion */
>if value<0 then value=value-1 /* Adjust for negative numbers */

You have to test whether the number is negative *before* you take only
the part before the period. Otherwise numbers between -1 and 0 result in
a value of 0 instead of -1.

Horst
- - - - - - - - - - - - - - - - - - - - - - -
Horst Kiehl - Internet h.p....@kfa-juelich.de

Keith Hedger

unread,
Sep 30, 1993, 10:38:15 AM9/30/93
to

Doesn't this function just 'trunc', not round ???

keith
--
=============================================================
= Keith Hedger| "Look at 'em...fuckin' normals... =
= | I hate 'em." =
=============================================================

George Shedlock

unread,
Oct 7, 1993, 2:31:59 PM10/7/93
to
Thanks to all who have responded to this query. I guess I should have
RTFM.
0 new messages