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

How to round floating point to 2 decimal places

506 views
Skip to first unread message

Don Ramm

unread,
Sep 24, 1996, 3:00:00 AM9/24/96
to

I've got a little DOS-based REXX interpreter that I've had for years and
which I use to write little programs to do little things.

Recently I wanted to find the difference between two numbers and report
the output with only two decimal places. I don't know how to round off
a number like:

3.6666667

to

3.67

Can someone tell me the function to do this or show me an example of a
'write' statement which will do the rounding automatically?

Just in case it isn't obvious, here is what I'm trying to do. Let's say
I have a simple REXX program named calcdiff.bat which is simply:

/* calcdiff.bat */
parse arg start stop
elapsed = stop - start
say elapsed
rndelp=round(elapsed,2)
say rndelp

So if I type:
run calcdiff .333333 1 ('run' is my batch file which runs REXX)

It displays:
.666667
.67

Where 'round' is my guess at the name of the function to round off a
floating point number.

--
Don Ramm, QUALCOMM Incorporated, San Diego CA
619-658-2597, dr...@qualcomm.com

Andre Doff

unread,
Sep 24, 1996, 3:00:00 AM9/24/96
to

In article <324802...@qualcomm.com>, Don Ramm <dr...@qualcomm.com> wrote:

DR> I don't know how to round off a number like:

DR> 3.6666667

DR> to

DR> 3.67

DR> Can someone tell me the function to do this or show me an example
DR> of a 'write' statement which will do the rounding automatically?

Format(number,,2)

Ave,

Andre Doff
ad...@ibm.net

Jonathan Nolting

unread,
Sep 24, 1996, 3:00:00 AM9/24/96
to

Try the FORMAT builtin.

/* indicates rexx */
trace o /* trace r/i is good - o is off */
/* */
/* */
values = '3.1234 3.5123 3.5789 3.6667'
do i = 1 to words(values)
say word(values,i) '=' format(word(values,i),1,2)
end

Ready;
test1
3.1234 = 3.12
3.5123 = 3.51
3.5789 = 3.58
3.6667 = 3.67
Ready;
sp con stop close

The 2nd parameter defines the number of positions to the left of the
decimal. The 3rd parameter defines the number of positions to the right
of the decimal with the rounding taking place automatically using the
next position right of the decimal as the base for the rounding.

Jon

dgo...@cfsrexx.com

unread,
Sep 25, 1996, 3:00:00 AM9/25/96
to

> Can someone tell me the function to do this or show me an
> example of a 'write' statement which will do the rounding
> automatically?

The FORMAT() built-in functions provides thae ability to round numbers
for you.

FORMAT( number[, [before][, [after]
[ ,expp][ ,expt]]]] )
Returns number rounded and formatted with before and
after specifying the size of the integer and fraction parts
respectively. Expp specifies the number of places for the
exponent and expt specifies the trigger point for the use
of exponential notation. If before is not large enough to
contain the integer part of number an error results.
Example: with x = 123.456
123.45 = FORMAT( x, 3, 2 )
123E+2 = FORMAT(x,3,2,1,1)

-----------------------------------------------------------------------
Dick Goran author, REXX Reference Summmary Handbook
C F S Nevada, Inc. Contributing Editor. OS/2 Magazine
953 E. Sahara Ave, Suite 9B [OS/2 Advisor]
Las Vegas, NV 89104-3012 CompuServe: 71154,2002
702-732-9616 Voice Internet: dgo...@cfsrexx.com
702-732-3847 FAX
http://www.cfsrexx.com
(see our new OS/2 game - CFSPoker)

Jean-Pierre Cabanié

unread,
Sep 26, 1996, 3:00:00 AM9/26/96
to

In message <324802...@qualcomm.com> - Don Ramm <dr...@qualcomm.com>
writes:
:>
:>I've got a little DOS-based REXX interpreter that I've had for years and
:> which I use to write little programs to do little things.
:>
:>Recently I wanted to find the difference between two numbers and report
:>the output with only two decimal places. I don't know how to round off
:>a number like:
:>
:>3.6666667
:>
:>to
:>
:>3.67
:>

Look at the Format instruction : Say Format(3.666667,,2) should display 3.67
From memory Format(number,before,after) where before and after are the
number of digits you want before and after the decimal point. Other parameters
may control the exponent part but I do not use them so memory is of no use...

JPierre


0 new messages