Re: Showing Written $ Amounts
Posted in 1995
Steven Jay Evans (sevans@vixa.voyager.net) wrote: : Any one know how to (Under Informix 4gl) chaanges ie. $24.50 into Twenty F four dollars and 50/100 : E-mail at sevans@vixa.voyager.net This is in the FAQ, but below is my version ======================================================================= Dennis J. Pimple dennisp@informix.com Opinions expressed Principal Consultant -------------------- are mine, and do not Informix Software Inc Voice: 303-850-0210 necessarily reflect Denver Colorado USA Fax: 303-779-4025 those of my employer. ####### ###### Dennis J. Pimple ## ## ## # Denver, CO ## ## ## # =================================================== ## ## ## ###### Client: Informix User's Group ## ## ## ## File: %M% SCCS: %I% %P% ####### ## ## Date: %G% %U% ## Program: Translate money to a CHAR text, # ## like for check printing ##### #---------------------------------------------------------------------# MAIN # Arguments: N/A # Purpose: For testing the money input. # Just enter an interrupt to quit. # REMOVE MAIN TO IMPLEMENT THIS MODULE INTO AN APPLICATION # Returns: N/A #---------------------------------------------------------------------# DEFINE mny MONEY DEFINE mstrg CHAR(80) CLEAR SCREEN WHILE TRUE PROMPT "Enter number 0 - 999999.99: " FOR mny LET mstrg = fmt_money(mny) MESSAGE mstrg CLIPPED END WHILE END MAIN #---------------------------------------------------------------------# FUNCTION fmt_money(mny) # Arguments: Amount of money to format # Purpose: Format money amount into a character string # Returns: CHAR(80) with the money string # Usage: LET charvar = fmt_money(moneyvar) # or: CALL fmt_money(moneyvar) RETURNING charvar # The return can change according to the maximum amount of the money # Hundreds of thousands (777,777.00) requires char(77) # Tens of thousands (77,777.00) requires char(61) # Thousands (7,777.00) requires char(53) # Hundreds (777.00) requires char(38) # if you never write a check in the hundreds, # you don't need this program # change the LET mlgth statement below to reflect any change you make # to rstrg #---------------------------------------------------------------------# DEFINE mny MONEY # amount passed DEFINE mstrg CHAR(9) # mny amount in a string DEFINE nstrg CHAR(3) # hundreds digitaly (eg: "090") DEFINE hstrg CHAR(30) # hundreds in char format DEFINE lgth SMALLINT # length measurment DEFINE rstrg CHAR(80) # Return string;CHANGE THIS LENGTH AS REQ DEFINE mlgth SMALLINT # length of mstrg # set mlgth to the max length of rstrg LET mlgth = 80 IF mny IS NULL THEN LET mny = 0 END IF LET mstrg = mny USING "<<<<<<.&&" LET rstrg = "" LET lgth = LENGTH(mstrg) IF lgth > 6 THEN # this number is in the thousands # caculate how many thousands and reduce the mstrg to hundreds CASE WHEN lgth = 9 # hundreds of thousands LET nstrg = mstrg[1,3] LET mstrg = mstrg[4,9] WHEN lgth = 8 # tens of thousands LET nstrg = mstrg[1,2] LET mstrg = mstrg[3,8] WHEN lgth = 7 # single thousands LET nstrg = mstrg[1] LET mstrg = mstrg[2,7] END CASE # nstrg now holds a number between 1 and 999 LET hstrg = fmt_hund(nstrg) LET rstrg = rstrg CLIPPED, " ", hstrg CLIPPED, " THOUSAND" END IF LET lgth = LENGTH(mstrg) IF lgth > 3 THEN # this number is in the hundreds CASE WHEN lgth = 6 # hundreds LET nstrg = mstrg[1,3] LET mstrg = mstrg[4,6] WHEN lgth = 5 # tens LET nstrg = mstrg[1,2] LET mstrg = mstrg[3,5] WHEN lgth = 4 # single digits LET nstrg = mstrg[1] LET mstrg = mstrg[2,4] END CASE # nstrg now holds a number between 1 and 999 LET hstrg = fmt_hund(nstrg) # add it to the (possible) thousands LET rstrg = rstrg CLIPPED, " ", hstrg CLIPPED END IF IF LENGTH(rstrg) = 0 THEN # if we still have nothing, there was no dollars LET rstrg = " ZERO" END IF # put the cents on as a fraction of hundreds LET rstrg = rstrg CLIPPED, " AND ", mstrg[2,3], "/100" # fill the left of the string with *** # REMOVE THE NEXT THREE LINES IF YOU DON'T WANT THIS DONE WHILE LENGTH(rstrg) < mlgth LET rstrg = "*", rstrg CLIPPED END WHILE RETURN rstrg END FUNCTION # fmt_money(mny) #---------------------------------------------------------------------# FUNCTION fmt_hund(nstrg) # Arguments: CHAR(3) string 000 - 999 with the possible hundreds # Purpose: Format hundreds amount into a character string # Returns: CHAR(30) with the hundreds format #---------------------------------------------------------------------# DEFINE nstrg CHAR(3) # "000" - "999" DEFINE n1 CHAR(1) # single digits tester DEFINE n2 CHAR(2) # double digits tester DEFINE lgth SMALLINT # length measurement DEFINE hstrg CHAR(30) # hundreds string to return DEFINE tstrg CHAR(13) # tens string to get from fmt_tens DEFINE x SMALLINT # a little counter LET lgth = LENGTH(nstrg) # since nstrg might be 000 or 010 or something like that, # we need to remove any leading zeros FOR x = 1 TO lgth IF nstrg[1] = "0" THEN LET nstrg = nstrg[2,lgth] ELSE EXIT FOR END IF END FOR LET lgth = LENGTH(nstrg) CASE WHEN lgth = 3 # hundreds LET n1 = nstrg[1] LET hstrg = fmt_digit(n1) LET hstrg = hstrg CLIPPED, " HUNDRED" LET n2 = nstrg[2,3] # get the tens part of this number LET tstrg = fmt_tens(n2) LET hstrg = hstrg CLIPPED, " ", tstrg CLIPPED WHEN lgth = 2 # tens LET n2 = nstrg CLIPPED LET hstrg = fmt_tens(n2) WHEN lgth = 1 LET n1 = nstrg CLIPPED LET hstrg = fmt_digit(n1) END CASE RETURN hstrg END FUNCTION # fmt_hund(nstrg) #---------------------------------------------------------------------# FUNCTION fmt_tens(n) # Arguments: CHAR(2) string 00 - 99 with the possible tens # Purpose: Format tens amount into a character string # Returns: CHAR(30) with the tens format #---------------------------------------------------------------------# DEFINE n CHAR(2) DEFINE tstrg CHAR(30) CASE WHEN n = "10" LET tstrg = "TEN" WHEN n = "11" LET tstrg = "ELEVEN" WHEN n = "12" LET tstrg = "TWELVE" WHEN n = "13" LET tstrg = "THIRTEEN" WHEN n = "14" LET tstrg = "FOURTEEN" WHEN n = "15" LET tstrg = "FIFTEEN@