[comp.lang.rexx] Dollarizing

lordbah@amusing.UUCP (Jeff Van Epps) (04/28/91)

What's an easier way to do this?

6 -> 6.00
6.2 -> 6.20
6.2324323 -> 6.23

i.e. fixed at two decimal places.

/*
 *	Format a number as a $$ value, with 2 digits after the decimal point.
 */

dollarize: procedure
	arg n
	dlen = index(n,'.')
	if dlen = 0 then
		do
		str = n || '.00'
		end
	else
		do
		n = left(n,20,'0')				/* lots of 0's after decimal */
		cents = right(left(n,dlen+2),2)
		str = left(n,dlen) || cents
		end
	return str

Looking at this, I see that it won't round the number either.  Bah.

--------------------------------------------------------------------
    Jeff Van Epps    uunet!amusing!lordbah

DSB100@psuvm.psu.edu (David Barr) (04/28/91)

How about using

  format(number,,2)  ?

This will automatically round the decimal to two decimal places, and
fill with zeros when necessary.

---
 David Barr - Penn State CAC Student Consultant, Student Programmer
 DSB100@psuvm.psu.edu     | dsbarr@endor.cs.psu.edu
 barr@barrstl.scol.pa.us  |...psuvax1!hogbbs!barrstl!barr

lordbah@amusing.UUCP (Jeff Van Epps) (04/28/91)

In article <91118.012747DSB100@psuvm.psu.edu> DSB100@psuvm.psu.edu (David Barr) writes:
> 
> How about using
> 
>   format(number,,2)  ?
> 
> This will automatically round the decimal to two decimal places, and
> fill with zeros when necessary.

Sounds good, but I can't find any reference to "format" in my ARexx
manual, and it says "function not found" when I try to run this.  Where
does "format" come from?

--------------------------------------------------------------------
    Jeff Van Epps    uunet!amusing!lordbah

David Barr <DSB100@psuvm.psu.edu> (04/29/91)

In article <lordbah.4876@amusing.UUCP>, lordbah@amusing.UUCP (Jeff Van Epps)
says:

>In article <91118.012747DSB100@psuvm.psu.edu> DSB100@psuvm.psu.edu (David
>Barr)
>writes:
>>
>> How about using
>>
>>   format(number,,2)  ?
>>
>> This will automatically round the decimal to two decimal places, and
>> fill with zeros when necessary.

>Sounds good, but I can't find any reference to "format" in my ARexx
>manual, and it says "function not found" when I try to run this.  Where
>does "format" come from?

Oops, I am using CMS Rexx, and it is a standard fuction in the IBM
package.  Too bad ARexx didn't implement it.  I find it very useful.

---
 David Barr - Penn State CAC Student Consultant, Student Programmer
 DSB100@psuvm.psu.edu     | dsbarr@endor.cs.psu.edu
 barr@barrstl.scol.pa.us  |...psuvax1!hogbbs!barrstl!barr

bzr10@ccc.amdahl.com (Bruce Richardson) (04/30/91)

Regarding the 'dollarizing' of numeric values, try using
format or trunc functions (format rounds up, trunc doesn't):
use       any_var=format(any_value,,2)
or        any_var=trunc(any_value,2)
I'm using CMS Rexx for this - from the help files, the syntax is:
      format(number(,(before)(,(after)(,(expp)(,expt)))))
and   trunc(number(,n))
the inner brackets are the optional items (ie should be in square
brackets).
                                     Regards, Bruce Richardson
<include the standard disclaimers, etc.>

CJS@psuvm.psu.edu (04/30/91)

Too bad ARexx doesn't have a FORMAT function.  I would have thought it
was in all implementations.

However, to simplify your "dollarize" routine:

   dollarize: procedure

      arg l '.' r .               /* left of decimal; right of decimal */

   return l'.'left(r, 2, '0')


To add rounding, perhaps this will do:

   dollarize: procedure
      arg l '.' r .               /* left of decimal; right of decimal */

      parse var r r +2 next +1 .

      if next <> '' then          /* more than 2 places? */
         if next > 4 then do      /* should we round up? */
            r = r + 1
            if r = 100 then do    /* did it overflow? */
               l = l + 1
               r = 0
            end
         end

   return l'.'left(r, 2, '0')

That rounding method is not quite perfect, and doesn't do negative
values correctly, but it is simple.

slr@eliot.UUCP (Stuart Roll/Development) (05/01/91)

In article <91119.112324DSB100@psuvm.psu.edu>, DSB100@psuvm.psu.edu (David Barr) writes:
> In article <lordbah.4876@amusing.UUCP>, lordbah@amusing.UUCP (Jeff Van Epps)
> says:
> 
> >In article <91118.012747DSB100@psuvm.psu.edu> DSB100@psuvm.psu.edu (David
> >Barr)
> >writes:
> >>
> >> How about using
> >>
> >>   format(number,,2)  ?
> >>
> >> This will automatically round the decimal to two decimal places, and
> >> fill with zeros when necessary.
> 
> >Sounds good, but I can't find any reference to "format" in my ARexx
> >manual, and it says "function not found" when I try to run this.  Where
> >does "format" come from?
> 

The standard definition of Rexx is found in the book "The REXX Language"
by M.F. Cowlishaw, who just by coincidence happens to be the author of the 
REXX language.  The book is in it's second edition now (Prentice Hall) and
contains the definition of "language version 4.00"

The CMS implementation is (last I checked) not up to 4.00 yet.  If you are
an ARexx user and the powers that be haven't seen fit to implement the whole
language, I'd suggest that you might want to jump up and down and throw fits
until they do.

I can't imagine them not implementing the format function.  I wonder what
else they left out?  (Your ARexx book may list the differences in an
appendix or something.)

-- 
Stuart Roll
...!uunet!eliot!slr

NIV@SLACVM.SLAC.STANFORD.EDU (05/01/91)

One really easy thing is

/* */
arg x
d = (x%.01)*.01
r = x//.01
if r >= .005 then d = d +.01
say d

This works well anyplace

desing@systems.cs.fsu.edu (Fred Desing) (05/01/91)

In article <91120.131127NIV@SLACVM.SLAC.STANFORD.EDU> NIV@SLACVM.SLAC.STANFORD.EDU writes:
>One really easy thing is
>
>/* */
>arg x
>d = (x%.01)*.01
>r = x//.01
>if r >= .005 then d = d +.01
>say d
>
>This works well anyplace

How about:

say trunc(x * 100 + .5) / 100

--
   _    /*      Fred G. Desing, FSU Computing Center Systems Group
   \'o.O'       Bitnet: desing@fsuavm  Internet: desing@avm.cc.fsu.edu
   =(___)=       _____________________________________
      U         | "Disk space -- the final frontier." |
 Bill says:     |______________________-Anonymous_____|
"I think, therefore: ackphthh"
--
   _    /*     Fred G. Desing, FSU Computing Center Systems Group
   \'o.O'      Bitnet: desing@fsuavm  Internet: desing@avm.cc.fsu.edu
   =(___)=      ---------------------------------------------------------
      U        |"Yea though I walk through the valley of the shadow of   |
 Bill says:    | death, I fear no evil.  'Cuz I'm the meanest, sneakiest |
   --------->> | most underhanded son-of-a-bitch in the whole valley."   |

terry@uts.amdahl.com (Lewis T. Flynn) (05/02/91)

In article <91119.112324DSB100@psuvm.psu.edu> DSB100@psuvm.psu.edu (David Barr) writes:
>In article <lordbah.4876@amusing.UUCP>, lordbah@amusing.UUCP (Jeff Van Epps)
>says:
>
>>In article <91118.012747DSB100@psuvm.psu.edu> DSB100@psuvm.psu.edu (David
>>Barr)
>>writes:
>>>
>>> How about using
>>>
>>>   format(number,,2)  ?
>>>
>>> This will automatically round the decimal to two decimal places, and
>>> fill with zeros when necessary.
>
>>Sounds good, but I can't find any reference to "format" in my ARexx
>>manual, and it says "function not found" when I try to run this.  Where
>>does "format" come from?
>
>Oops, I am using CMS Rexx, and it is a standard fuction in the IBM
>package.  Too bad ARexx didn't implement it.  I find it very useful.
>

It's in the original language (as documented in Cowlishaw's book), but
many implementations leave out things (like CMS leaves out charout,
charin, etc.).

Terry

rrezaian@austral.UUCP (Russell Rezaian) (05/04/91)

In article <1991Apr30.191547.6197@eliot.uucp> slr@eliot.UUCP (Stuart Roll/Development) writes:
[..]
>The standard definition of Rexx is found in the book "The REXX Language"
>by M.F. Cowlishaw, who just by coincidence happens to be the author of the 
>REXX language.  The book is in it's second edition now (Prentice Hall) and
>contains the definition of "language version 4.00"
[...]
>I can't imagine them not implementing the format function.  I wonder what
>else they left out?  (Your ARexx book may list the differences in an
>appendix or something.)
[...]
The omissions in the ARexx function libraries are puzzling to say the
least...  Format doesn't seem to be obvious, (check the update docs that
came with 1.15, he might have added it...) and even stranger there is one
example that drives a friend of mine nuts.  It is so strange because only
one of a (usually) matched pair of functions was implemented:  to wit, case
conversion.  There is a toupper function, but NO tolower.  I have never
needesd these, but a friend of mine REALLY wants a tolower function for use
in one of his larger programs.

I don't claim to know what kind of work went into implementing AREXX, I am
pretty impressed by what I have seen, but there are a few little perplexing
things still floating around...
--
Russell Rezaian			|  rrezaian@austral.UUCP
P.O. Box 479			|  rrezaian@amiganet.chi.il.us
Naperville, Il.  60566-0479	|  "One is best punished for one's
USA				|   Virtues."  Nietzsche.

slr@eliot.UUCP (Stuart Roll/Development) (05/07/91)

In article <rrezaian.1271@austral.UUCP>, rrezaian@austral.UUCP (Russell Rezaian) writes:
>                                    ...........It is so strange because only
> one of a (usually) matched pair of functions was implemented:  to wit, case
> conversion.  There is a toupper function, but NO tolower.  I have never
> needesd these, but a friend of mine REALLY wants a tolower function for use
> in one of his larger programs.
> 

  Strange.  The REXX 'translate' function can do both uppercase and lowercase
conversions as well as a lot of other neat things including byte reordering.
I wonder why they felt like adding to it?  As much as I like C, I expect they
are trying to C-ize REXX.  One of the nice things about REXX is that it has 
about 67 'standard' library functions.  ANSI C, by comparison, has about 250
standard functions.  REXX has a few quirks that I don't like either, but for
what it is, it's a great language.  I'd hate to see it turned into another ADA
(all things to all programmers).
-- 

| Stuart Roll            | "Why can't life's problems hit us when we're     |
| Unitech Software Inc.  | seventeen and know everything?"                  |
| ...uunet!eliot!slr     |                  -- A.C. Jolly                   |
| eliot!slr@uunet.uu.net |                                                  |