############################################################################
#
#	File:     currency.icn
#
#	Subject:  Procedures for formatting currency
#
#	Author:   Robert J. Alexander
#
#	Date:     September 21, 1993
#
############################################################################
#
#   This file is in the public domain.
#
############################################################################
#
#  currency() -- Formats "amount" in standard American currency format.
#  "amount" can be a real, integer, or numeric string.  "width" is the
#  output field width, in which the amount is right adjusted.  The
#  returned string will be longer than "width" if necessary to preserve
#  significance.  "minus" is the character string to be used for
#  negative amounts (default "-"), and is placed to the right of the
#  amount.
#
############################################################################

procedure currency(amount,width,minus,decPlaces,minDollarDigits,
      currencySign,decimalPoint,comma)
   local sign,p
   amount := real(amount) | fail
   /width := 0
   /minus := "-"
   /decPlaces := 2
   /minDollarDigits := 1
   /currencySign := "$"
   /decimalPoint := "."
   /comma := ","
   if amount < 0.0 then {
      sign := minus
      amount := -amount
      }
   else sign := repl(" ",*minus)
   amount := (integer(amount * 10.0 ^ (decPlaces + 1)) + 5)[1:-1]
   amount := right(amount,*amount < decPlaces + minDollarDigits,"0")
   p := *amount - decPlaces + 1
   amount[p:p] := decimalPoint
   while (p -:= 3) > 1 do amount[p:p] := comma
   amount := currencySign || amount || sign
   amount := right(amount,*amount < width)
   return amount
end
