PRINT USING
Suppose you want to add $12.47 to $1.03. The correct answer is $13.50. This almost works:
PRINT 12.47 + 1.03
It makes the computer print:
13.5
But instead of 13.5, we should try to make the computer print 13.50.
This command forces the computer to print 13.50:
PRINT USING "##.##"; 12.47 + 1.03
The “##.##” is called the picture or image or format: it says to print two characters, then a decimal point, then two digits. The computer will print:
13.50
This command puts that answer into a sentence:
PRINT USING "You spent ##.## at our store"; 12.47 + 1.03
The computer will print:
You spent 13.50 at our store
Rounding This program makes the computer divide 300 by 7 but round the answer to two decimal places:
CLS
PRINT USING "##.##"; 300 / 7
When the computer divides 300 by 7, it gets 42.85714, but the format rounds the answer to 42.86. The computer will print:
42.86
Multiple numbers Every format (such as “###.##”) is a string. You can replace the format by a string variable:
CLS
a$ = "###.##"
PRINT USING a$; 247.91
PRINT USING a$; 823
PRINT USING a$; 7
PRINT USING a$; -5
PRINT USING a$; -80.3
The computer will print:
247.91
823.00
7.00
-5.00
-80.30
When the computer prints that column of numbers, notice that the computer prints the decimal points underneath each other so that they line up. So to make decimal points line up, say PRINT USING instead of just PRINT.
To print those numbers across instead of down, say this:
PRINT USING "###.##"; 247.91; 823; 7; -5; -80.3
It makes the computer print 247.91, then 823.00, etc., like this:
247.91823.00 7.00 -5.00-80.30
Since the computer prints those numbers so close together, they’re hard to read. To make the computer insert extra space between the numbers, widen the format by putting a fourth “#” before the decimal point:
PRINT USING "####.##"; 247.91; 823; 7; -5; -80.3
Then the computer will print:
247.91 823.00 7.00 -5.00 -80.30
If you say —
PRINT USING "My ## pals drank ###.# pints of gin"; 24; 983.5
the computer will print:
My 24 pals drank 983.5 pints of gin
Oversized numbers Suppose you say:
PRINT USING "###.##"; 16238.7
The computer tries to print 16238.7 by using the format “###.##”. But since that format allows just three digits before the decimal point, the format isn’t large enough to fit 16238.7. So the computer must disobey the format. But the computer also prints a percent sign, which means, “Warning! I am disobeying you!” Altogether, the computer prints:
%16238.70
Final semicolon At the end of the PRINT USING statement, you can put a semicolon:
CLS
PRINT USING "##.##"; 13.5;
PRINT "credit"
Line 2 makes the computer print 13.50. The semicolon at the end of line 2 makes the computer print “credit” on the same line, like this:
13.50credit
Advanced formats Suppose you’re running a high-risk business. On Monday, your business runs badly: you lose $27,931.60, so your “profit” is minus $27,931.60. On Tuesday, your business does slightly better than break-even: your net profit for the day is $8.95.
Let’s make the computer print the word “profit”, then the amount of your profit (such as -$27,931.60 or $8.95), then the word “ha” (because you’re cynical about how your business is going).
You can do that printing in several ways. Let’s explore them.…
If you say —
CLS
a$ = "profit######.##ha"
PRINT USING a$; -27931.6
PRINT USING a$; 8.95
the computer will print:
profit-27931.60ha
profit 8.95ha
If you change the format to “profit###,###.##ha”, the computer will insert a comma if the number is large:
profit-27,931.60ha
profit 8.95ha
If you change the format to “profit+#####.##ha”, the computer will print a plus sign in front of any positive number:
profit-27931.60ha
profit +8.95ha
To print a negative number, the computer normally prints a minus sign before the number. That’s called a leading minus. You can make the computer put the minus sign after the number instead; that’s called a trailing minus. For example, if you change the format to “profit######.##-ha”, the computer will print a minus sign AFTER a negative number (and no minus after a positive number), like this:
profit27931.60-ha
profit 8.95 ha
Normally, a format begins with ##. If you begin with $$ instead (like this: “profit$$#####.##ha”), the computer will print a dollar sign before the digits:
profit-$27931.60ha
profit $8.95ha
If you begin with ** (like this: “profit**#####.##ha”), the computer will print asterisks before the number:
profit*-27931.60ha
profit******8.95ha
If you begin with **$ (like this: “profit**$#####.##ha”), the computer will print asterisks and a dollar sign:
profit*-$27931.60ha
profit******$8.95ha
When you’re printing a paycheck, use the asterisks to prevent the employee from enlarging his salary. Since the asterisks protect the check from being altered, they’re called check protection.
You can combine several techniques into a single format. For example, you can combine the comma, the trailing minus, and the **$ (like this: “profit**$##,###.##-ha”), so that the computer will print:
profit**$27,931.60-ha
profit*******$8.95 ha
If you change the format to “profit##.#####^^^^ha”, the computer will print numbers by using E notation:
profit-2.79316E+04ha
profit 8.95000E+00ha