PRINT USING Procedures



Similar documents
Decimal form. Fw.d Exponential form Ew.d Ew.dEe Scientific form ESw.d ESw.dEe Engineering form ENw.d ENw.dEe Lw. Horizontal

Exponents. Learning Objectives 4-1

LINEAR INEQUALITIES. less than, < 2x + 5 x 3 less than or equal to, greater than, > 3x 2 x 6 greater than or equal to,

Name: Class: Date: 9. The compiler ignores all comments they are there strictly for the convenience of anyone reading the program.

Solutions of Linear Equations in One Variable

Negative Integral Exponents. If x is nonzero, the reciprocal of x is written as 1 x. For example, the reciprocal of 23 is written as 2

A positive exponent means repeated multiplication. A negative exponent means the opposite of repeated multiplication, which is repeated

Substitute 4 for x in the function, Simplify.

.NET Standard DateTime Format Strings

SUNY ECC. ACCUPLACER Preparation Workshop. Algebra Skills

To convert an arbitrary power of 2 into its English equivalent, remember the rules of exponential arithmetic:

The GMAT Guru. Prime Factorization: Theory and Practice

Chapter 4. Polynomial and Rational Functions. 4.1 Polynomial Functions and Their Graphs

Streaming Lossless Data Compression Algorithm (SLDC)

Algebra. Exponents. Absolute Value. Simplify each of the following as much as possible. 2x y x + y y. xxx 3. x x x xx x. 1. Evaluate 5 and 123

Custom Linetypes (.LIN)

COMP 250 Fall 2012 lecture 2 binary representations Sept. 11, 2012

Computer Science 281 Binary and Hexadecimal Review

1 Description of The Simpletron

Binary Adders: Half Adders and Full Adders

Commission Accounting User Manual

Lecture 2. Binary and Hexadecimal Numbers

A Short Guide to Significant Figures

Solution for Homework 2

NUMBER SYSTEMS. William Stallings

Version. General Certificate of Education (A-level) January Mathematics MPC3. (Specification 6360) Pure Core 3. Final.

Q&As: Microsoft Excel 2013: Chapter 2

Excel: Introduction to Formulas

Training Manual. Pre-Employment Math. Version 1.1

5.1 Radical Notation and Rational Exponents

Formatting Numbers with C++ Output Streams

Secrets of printf. 1 Background. 2 Simple Printing. Professor Don Colton. Brigham Young University Hawaii. 2.1 Naturally Special Characters

Lab 9 Access PreLab Copy the prelab folder, Lab09 PreLab9_Access_intro

Payroll Import. Version Main screen for Payroll Import.

CHAPTER 5 Round-off errors

grep, awk and sed three VERY useful command-line utilities Matt Probert, Uni of York grep = global regular expression print

Importing Xerox LAN Fax Phonebook Data from Microsoft Outlook

Chapter 4: Computer Codes

ABOUT THIS DOCUMENT ABOUT CHARTS/COMMON TERMINOLOGY

Recall the process used for adding decimal numbers. 1. Place the numbers to be added in vertical format, aligning the decimal points.

3. Solve the equation containing only one variable for that variable.

Section 1-4 Functions: Graphs and Properties

UNDERSTANDING ALGEBRA JAMES BRENNAN. Copyright 2002, All Rights Reserved

YOU MUST BE ABLE TO DO THE FOLLOWING PROBLEMS WITHOUT A CALCULATOR!

Utility Software II lab 1 Jacek Wiślicki, jacenty@kis.p.lodz.pl original material by Hubert Kołodziejski

C++ Programming: From Problem Analysis to Program Design, Fifth Edition. Chapter 3: Input/Output

IMPORT GUIDE ASCII File to Trial Balance CS

MATLAB Programming. Problem 1: Sequential

Chapter 4: Exponential and Logarithmic Functions

Quick Reference ebook

Integrating with Mathematica

28 Simply Confirming Onsite

Stacks. Linear data structures

Core Maths C1. Revision Notes

Section 3-3 Approximating Real Zeros of Polynomials

Homework 2 Solutions

The numerical values that you find are called the solutions of the equation.

3 Data Properties and Validation Rules

MS Access: Advanced Tables and Queries. Lesson Notes Author: Pamela Schmidt

2+2 Just type and press enter and the answer comes up ans = 4

The string of digits in the binary number system represents the quantity

Section 6-3 Double-Angle and Half-Angle Identities

MPE Review Section III: Logarithmic & Exponential Functions

Bachelors of Computer Application Programming Principle & Algorithm (BCA-S102T)

NPV Versus IRR. W.L. Silber We know that if the cost of capital is 18 percent we reject the project because the NPV

Review of Fundamental Mathematics

ExamView Dynamic Questions Training Guide for PC and Mac Users

Commonly Used Excel Formulas

Working with whole numbers

Pemrograman Dasar. Basic Elements Of Java

Base Conversion written by Cathy Saxton

We can express this in decimal notation (in contrast to the underline notation we have been using) as follows: b + 90c = c + 10b

About the Gamma Function

2.2 Scientific Notation: Writing Large and Small Numbers

Part 1 Expressions, Equations, and Inequalities: Simplifying and Solving

EE 261 Introduction to Logic Circuits. Module #2 Number Systems

Continued Fractions and the Euclidean Algorithm

Business and Economic Applications

Sources: On the Web: Slides will be available on:

Marks-to-Market in U.S. Treasury Futures and Options: Conventions for Computing Variation Margin Amounts

F ahrenheit = 9 Celsius + 32

26 Integers: Multiplication, Division, and Order

Polynomials and Factoring

HOW TO MAKE A TABLE OF CONTENTS

Creating Basic Excel Formulas

23. RATIONAL EXPONENTS

Arithmetic Progression

Numeral Systems. The number twenty-five can be represented in many ways: Decimal system (base 10): 25 Roman numerals:

Section 1.3 P 1 = 1 2. = P n = 1 P 3 = Continuing in this fashion, it should seem reasonable that, for any n = 1, 2, 3,..., =

Guide to Performance and Tuning: Query Performance and Sampled Selectivity

Elfring Fonts, Inc. PCL MICR Fonts

Second Order Linear Nonhomogeneous Differential Equations; Method of Undetermined Coefficients. y + p(t) y + q(t) y = g(t), g(t) 0.

Time Clock Import Setup & Use

Directions for the AP Invoice Upload Spreadsheet

Regular Expressions. General Concepts About Regular Expressions

Number Representation

CSI 333 Lecture 1 Number Systems

Algebra 2 Unit 8 (Chapter 7) CALCULATORS ARE NOT ALLOWED

Provider Commission Provider Initial % of fund value. Fund based start month % % % % 0.5% % 0.

Transcription:

-1 CHAPTER PRINT USING Procedures True BASIC normally prints numbers in a form convenient for most purposes. But on occasion you may prefer a more elaborate form. For eample, you may want to print financial quantities with two decimal places (for cents) and, possibly, with commas inserted every three digits to the left of the decimal point. PRINT USING provides a way to print numbers in this and almost any other form. Here is an eample of the PRINT USING statement. PRINT USING format$:, y, z Format$ is a string of characters that contains the instructions to PRINT USING for formatting the printing of, y, and z. This string is called a format string. It may be a string variable (as shown above), a quoted-string, or a more general string epression. PRINT USING also allows one to print strings centered or right-justified, as well as left-justified. (The normal PRINT statement prints both strings and numbers left-justified within each print zone; see Chapter 34, the PRINT statement.) The function USING$ duplicates the PRINT USING statement almost eactly but returns the result as a string rather than printing it on the screen. For eample, the following two statements yield the same output as the preceding PRINT USING statement. LET outstring$ = using$(format$,, y, z) PRINT outstring$ The USING$ function allows you to modify or save the string outstring$ before printing it. You can also use this function with WRITE and PLOT TEXT statements. (See Chapter 32, for the USING$ function.) We will first eamine how to format numerical output.

-2 PC Pro Version Reference Manual Formatting Numbers The basic idea of a format string is that the symbol # stands for a digit position. For eample, let us compare the output resulting from two similar PRINT statements, the first a normal PRINT statement and the second employing USING. PRINT PRINT USING "###": In the following table, the symbol is used to denote the left margin and does not actually appear on the screen. PRINT PRINT USING "###": - ------- -------------------- 1 1 1 12 12 12 123 123 123 1234 1234 *** We notice several things. Without USING, the number is printed left-justified with a leading space for a possible minus sign, and occupying only as much space as needed. With USING, the format string ### specifies a field length of eactly three characters. The number is printed right-justified in this field. If the field is not long enough to print the number properly, asterisks are printed instead, the unformatted value (here, of ) is printed on the net line and printing continues on the following line. If all you need to do is to print integer numbers in a column but with right-justification, then the preceding eample will suffice. Printing financial quantities so that the decimal points are aligned is important. Also, you may want to print two decimal places (for the cents) even when they are 0. The following eample shows how to do this. (In order to print negative numbers, the format string must start with a minus sign.) PRINT PRINT USING "-##.##": -- ------- ----------------------- 1 1 1.00 1.2 1.2 1.20-3.57-3.57-3.57 1.238 1.238 1.24 123 123 ****** 0 0.00-123 -123 ****** Notice that two decimal places are always printed, even when they consist of zeroes. Also, the result is first rounded to two decimals. If the number is negative, the minus sign occupies the leading digit position. If the number is too long to be printed properly

PRINT USING Procedures -3 (possibly because of a minus sign), asterisks are printed instead, the unformatted value is printed on the net line, and printing continues on the following line. Financial quantities are often printed with a leading dollar sign ($), and with commas forming three-digit groups to the left of the decimal point. The following eample shows how to do this with PRINT USING. PRINT USING "$#,###,###.##": -- ------------------------------ 0 $.00 1 $ 1.00 1234 $ 1,234.00 1234567.89 $1,234,567.89 1e6 $1,000,000.00 1e7 ************* Notice that the dollar sign is always printed and is in the same position (first) in the field. Also, the separating commas are printed only when needed. You might sometimes want the dollar sign ($) to float to the right, so that it appears net to the number, avoiding all those blank spaces between the dollar sign and the first digit in the preceding eample. The following eample shows how to do this. PRINT USING "$$$$$$$#.##": -- ---------------------------- 0 $.00 1 $1.00 1234 $1234.00 1234567.89 $1234567.89 Digit positions represented by $ instead of # cannot surround or be net to commas. In the previous two eamples, no negative amounts can be printed since the format string does not start with or contain a minus sign. The format string can also allow leading zeroes to be printed, or to be replaced by asterisks (*). You might find the latter useful if you are preparing a check-writing program. PRINT USING "$%,%%%,%%%.##": -- ------------------------------ 0 $0,000,000.00 1 $0,000,001.00 1234 $0,001,234.00 1234567.89 $1,234,567.89

-4 PC Pro Version Reference Manual PRINT USING "$*,***,***.##": -- ------------------------------ 0 $*********.00 1 $********1.00 1234 $****1,234.00 1234567.89 $1,234,567.89 You can also format numbers using scientific notation. Because scientific notation has two parts, the decimal-part and the eponent-part, the format string must also have two parts. The decimal-part follows the rules already illustrated. The eponent-part consists of from three to five carets (^) that must immediately follow the decimal-part. The following eample shows how. PRINT USING "+#.#####^^^^": -- ----------------------------- 0 +0.00000e+00 123.456 +1.23456e+02 -.001324379-1.32438e-03 7e +7.00000e+.5e100 +5.00000e+99 5e100 ************ Notice that a leading plus sign (+) in the format string guarantees that the sign of the number will be printed, even when the number is positive. Notice also that the last number cannot be formatted because the eponent part would have been 100, which requires an eponent field of five carets. Notice also that if there are more carets than needed for the eponent, leading zeroes are inserted. Finally, notice that trailing zeroes in the decimal part are printed. Floating Characters You ll notice that one of the previous eamples includes several $, but that only one of them is actually printed. It is printed just to the left of the left-most non-zero digit, but always within the positions given by the sequence of $. We say that the sequence of $ defines a floating region and that the spot where the $ is printed floats within this region. Besides the $, the plus sign (+) and the minus sign (-) can also define floating regions. The rules are: 1. You can use either zero, one, or two different floating characters ( + and - cannot both appear, and neither can commas.) 2. You can repeat the first (or only) floating character an arbitrary number of times, but not the second.

PRINT USING Procedures -5 3. Zero to two different floating characters generate a sequence of zero to two characters called a header, as follows: The Floating Header First Second Positive Negative $ + "$+" "$ " $ "$ " "$ " $ none "$" error + $ "+$" " $" + none "+" " " $ " $" " $" none " " " " none none "" error Notice that the header contains the same number of characters as the number of different floating characters. 4. The zero to two character header will be printed as far to the right as possible within the floating region. 5. The numerical value s leading digits can overflow into the floating region, thereby pushing the header to the left. 6. If the numerical value eceeds the total space provided, the entire space is filled with asterisks. The following eample illustrates some of these rules. PRINT PRINT USING "$$$$$$$-#,###.##": ------- --------------------------------- 0 $.00 1 $ 1.00-1 $- 1.00 4321.5 $ 4,321.50-4321.5 $-4,321.50 1.23456789e+7 $ 12345,678.90-1.23456789e7 $-12345,678.90 1000000000 $ 1000000,000.00-1000000000 $-1000000,000.00 Notice that the $ is never printed outside the floating region. A place is allocated for the minus sign. The leading digits of the numerical value can overflow into the floating region, which does not (and cannot) contain commas.

-6 PC Pro Version Reference Manual Formatting Strings Strings can also be formatted through PRINT USING or the function USING$, although there are fewer options for strings than for numbers. Strings can be printed in the formatted field either left-justified, centered, or right-justified. As with numbers, if the string is too long to fit, then asterisks are printed, the actual string is printed on the net line, and printing continues on the following line. The following eample shows several cases. USING String to be Printed string "Ok" "Hello" "Goodbye" ------ ------ ------- --------- "<####" Ok Hello ******* "#####" Ok Hello ******* ">####" Ok Hello ******* Notice that if centering cannot be eact, the etra space is placed to the right. Any numeric field can be used to format a string, in which case the string is centered. This is especially valuable for printing headers for a numeric table. The following eample shows how you can format headers using the same format string we used earlier for numbers. s$ PRINT USING "$#,###,###.##": s$ ------------- ------------------------------- "Cash" Cash "Liabilities" Liabilities "Accounts Receivable" ************* Multiple Fields and Other Rules A PRINT USING format string can contain several format items. For eample, to print a table of sines and cosines, we may want to use: LET format$ = "-#.### -#.###### -#.######" PRINT USING format$:, sin(), cos() The value of will then be printed to three decimals, while the values of the sine and cosine will be printed to si decimals. Notice also that spaces between the format items will give equal spaces between the columns in the printed result. If there are more format items than there are values (numbers or strings) to be printed, the rest of the format string starting with the first unused format item is ignored. If there are fewer format items than values to be printed, the format string is reused, but starting on the net line. Thus, PRINT USING " -#.#####": 1.2, 2.3, 3.4

PRINT USING Procedures -7 will yield: 1.20000 2.000 3.40000 Literals in Format Strings We have just seen that spaces between format items in a format string are printed. That is, if there are four spaces, the four spaces are printed. The same is true for more general characters that may appear between format items. The rule is simple: you can use any sequence of characters between format items ecept the special formatting characters. The characters you use will then be printed. The special formatting characters are: # % * < > ^. + -, $ The following eample illustrates this use. will yield: PRINT USING "#.## plus #.## equals #.##": 1.2, 2.3, 1.2+2.3 1.20 plus 2. equals 3.50 If there are fewer values than format items, the unused format items are ignored, but the last intervening literal string is printed. Thus, will yield PRINT USING "#.## plus #.## equals #.##": 1.2, 2.3 1.20 plus 2. equals If you need to have one of the special formatting characters appear in the output for eample, if you want to have a final period, as in the last eample you can simply add a one-character field to the format string and add the quoted-string. to the PRINT statement. Thus, will yield LET = 1.2 LET y = 2.3 PRINT USING "#.## plus #.## equals #.## #":, y, +y, "." 1.20 plus 2. equals 3.50.

-8 PC Pro Version Reference Manual True BASIC employs two forms of the PRINT USING and Using$ functions. The first is the version used since version 1.0 of the Language System. This is the default in the professional version. The other is a completely ANSI-standard version, which is slightly more restrictive. If you wish to use this version, you may include the statement OPTION USING ANSI in your program before the first USING statement that you wish to conform to the ANSI standard. To switch back to the default version, you may include the statement OPTION USING TRUE before the first USING statement that you wish to conform to the True BASIC specifications. Eceptions The following runtime errors can arise: Eceptions: 8201 Badly formed USING string. 8202 No USING item for output. 8203 USING value too large for field. (nonfatal) 8204 USING eponent too large for field. (nonfatal)