REXX Tips & Tricks, Version 2.80


Inf-HTML [About][Toc][Index] 0.9b (c) 1995 Peter Childs


Formatting numbers





 
/* sample routine to format a number into the format                  */
/*  nnn.nnn.nnn                                                       */
/* and vice versa                                                     */
/*                                                                    */
/* Source: I found routines of this type in various messages on the   */
/*         IBM REXX forums                                            */
/*         (see also EdmREXX - misc. functions for REXX)              */
/*                                                                    */
  do forever

    call CharOut , "Enter a number (RETURN to end): "
    thisNumber = lineIn()
    if thisNumber = "" then
      leave
    else
    do
      thatNumber = FormatNumber( thisNumber )
      call LineOut , "Result of FormatNumber( " || thisNumber || " ) is " ,
                     thatNumber

      call LineOut , "Result of UnFormatNumber( " || thatNumber || " ) is " ,
                     UnFormatNumber( thatNumber )
    end /* else */
  end /* do forever */

exit

/* ------------------------------------------------------------------ */
/* function: Format a number like 123456789.44 into the format        */
/*           123,456,789.44                                           */
/*                                                                    */
/* call:     FormatNumber number_to_format                            */
/*                                                                    */
/* where:    number_to_format                                         */
/*                                                                    */
/* returns:  the formatted number                                     */
/*                                                                    */
/* note:     works for all numbers up to (but not including)          */
/*           1.000,000,000,000,000.0                                  */
/*                                                                    */
/* Author:   Graham Ewart                                             */
/*                                                                    */
FormatNumber: PROCEDURE expose (exposeList)
  parse value arg(1) with whole "." decs

  formattedNumber = strip( reverse( translate( "abc,def,ghi,jkl,mno",,
                                   reverse(whole),,
                                   "abcdefghijklmno",",")),"L",",")

  if decs <> "" then
    formattedNumber = formattedNumber || "." || decs
RETURN formattedNumber

/* ------------------------------------------------------------------ */
/* function: Unformat a number like 123,456,789.44 into the format    */
/*           123456789.44                                             */
/*                                                                    */
/* call:     UnFormatNumber number_to_unformat                        */
/*                                                                    */
/* where:    number_to_unformat                                       */
/*                                                                    */
/* returns:  the unformatted number                                   */
/*                                                                    */
/* note:     works for all numbers                                    */
/*                                                                    */
UnFormatNumber: PROCEDURE

RETURN space( translate( arg(1), " ", "," ) , 0, )


  

Inf-HTML End Run - Successful