Authors
Publication
Pub Details
Date
Pages
BASIC Terms
How to Get Straight Columns When Working with Dollar Amounts
Once again, programming proves that necessity is the mother of invention. When I began writing my credit card payment program, I knew that I would need a subroutine that would display a ‘dollars and cents’ number on the screen so that all of the numbers would line up in nice, neat, even columns. I also wanted to print any numbers without cents to be displayed with a “.00” after the dollar amount (remember, TS1000’s and TS2068’s would PRINT 5.00 as 5) I also wanted it to fill in for numbers like “.4” to be “.40”. I knew that I needed a special & universal routine to do this, thus subroutine 4000 was born.
Subroutine 4000 is written for the TS2068 and will also work for the TS1000/1500. The routine requires the following values as input before it is called:Pos This is the right most column (screen position) where the cents digit is printed. In other words, the numbers are Right-Justified. Num This is the ‘real’ amount you wish to be printed. Siz This is the maximum number of digits plus the decimal that can/will be displayed and is used to blank-out previous numbers printed at that location. X This variable holds the value of the row where the number is to be printed on the screen (0..21).
(NOTE: Each of the above variables is an integer.)
Now that the input parameters are covered, here is the actual subroutine 4000 (a.k.a. Display Dollars and Cents):
BASIC Listing — Subroutine 4000: Display Dollars & Cents
4000 REM Display Dollars & Cents
4010 LET v$=""
4020 LET v$=STR$ num
4030 IF num=INT num THEN LET v$=v$+".00": GO TO 4050
4040 IF ((num+1)*10)=INT ((num+1)*10) THEN LET v$=v$+CHR$ (48)
4050 LET len=LEN v$: LET here=pos-len
4060 FOR h=pos TO (pos-siz-1) STEP -1: PRINT AT x,h;CHR$ 32
4070 NEXT h
4090 PRINT AT x,here;v$
4099 RETURN
Optional Statements
To have the routine print blanks instead of “0.00” for a zero value, add:
4080 IF num=0 THEN GO TO 4099
(prints blanks for zero values)
To handle most rounding problems that occur from percentages and similar calculations, add:
4005 LET num=num+.005: LET num=num*100: LET num=INT num: LET num=num/100
(handles most rounding problems)
Another optional statement blanks out the print position with spaces from left to right instead of right to left as in lines 4060 and 4070. To do this use the following lines instead:
4060 LET e$=""
4065 FOR h=(pos-siz-1) TO pos: LET e$=e$+CHR$ (32): NEXT h
4070 PRINT AT x,(pos-siz-1);e$
Taking a look at the program, num is first converted to a string and is then checked to see if a “0” or “.00” needs to be added to the end of the string for trailing zeros.
Note in line 4040 that one (1) is added to num on both sides of the comparison. This is because of a quirk in the TS2068 such that a value of 0.6 would fall through the IF condition, when it otherwise should have been caught! But by adding one (1) to both sides, the IF condition will work as it should.
Next, we find the length of the string, determine how many print positions to the left of pos we must move to begin printing our number so that the last digit ends up in the pos column.
Line 4060 is used to blank out any previous numbers at the positions where we want the new number to reside. Let me warn you, when executed, this line and the subsequent printing of the number is Different! (This is why I included an optional way to blank the position).
Lines 4090 and 4099 finally print the number and return from the subroutine.
The Optional Statement 4080 causes spaces to be printed instead of a value of zero. The optional line 4005 is used to handle any rounding problems which occur from percentages, etc. when working with dollar amounts. It assumes we wish to round up to the nearest penney any time we have a value of $.005 or greater.
I hope this routine is as useful to you as it is to me for your business programming.
Products
Media
Image Gallery
Source Code
Note: Type-in program listings on this website use ZMAKEBAS notation for graphics characters.