Visual Basic 2008 Programming: Business Applications with a Design Perspective
Chapter 1 Chapter 2 Chapter 3 Chapter 4 Chapter 5 Chapter 6 Chapter 7 Chapter 8 Chapter 9
Chapter 10 Chapter 11 Chapter 12 Chapter 13 Chapter 14 Appendix A Appendix B Appendix C Home
Last change: February 13 2016 18:48:19.

 Chapter in PDF

Table of Contents

Chapter 4: Data, Operations, and Built-In Functions
Last change: February 13 2016 18:47:36.

The Format Function

The Format function provides a wide variety of formatting capabilities. The syntax is as follows:

TextStringVariable = Format(Expression, Formatting String)

where Expression represents any valid expression that evaluates to either a string or a number to be formatted, and Formatting string represents a string that specifies the format.

The formatting string can be a user-defined string or a name recognized by VB. For example, VB recognizes the name Standard, which will show the number with commas (as thousand separators) and a period with two decimal places; for example, Format(3000, “Standard”) will give the result: 3,000.00. The following table lists selected named numeric formats.

Format Name Meaning Example Code Result
Currency, C, or c Display the dollar sign before the number with thousand separator and two digits to the right of the decimal point. Format(3000, “Currency”) $3,000.00
Fixed, F, or f Display at least one digit to the left and two digits to the right of the decimal point. Format(3000.1, “Fixed”) 3000.10
General Number, G or g Format(3000, “G”) without thousand separator Format(3000,”G”) 3000
Percent Display number multiplied by 100 with a percentage sign (%) appended to the right with two digits to the right of the decimal point. Format(0.25, “Percent”) 25.00%
Scientific E, or e Use standard scientific notation. Format(1234, “Scientific”) 1.234E+03
Standard, N, or n Display numbers with thousand separator and two digits to the Right of the decimal point. Format(3000,”Standard”) 3,000.00

You can also provide your own user-defined formatting string. The following table gives a selected list of characters that you can use to form the formatting string.

Character Explanation Code example Result
# Place holder; either the number or nothing Format(5, “###”) 5
0 Place holder; either the number or 0 Format(5, “000”) 005
. Decimal place holder Format(5,”###.00”) 5.00
, Thousand separator Format(5000, “#,###.00”) 5,000.00
+, -, $, (, ) Literal Format(5000,”$#,###.00”) $5,000.00
% Percent (multiplied by 100) Format(5, ”###.00%”) 500.00%

Back to the example, you can use the Format function with the Standard format to display all the amounts in the form. To do so, the statement to display (net) cash should be changed to the following:

txtNetCash.Text = Format(Cash, “Standard”)

All other statements to display the amounts should be changed similarly. You will notice that “Standard” appears in several lines of code. You can, instead, use a symbolic constant, such as FmtStandard, in place of the literal “Standard.” You can declare the constant next to the declaration of credit card processing rate:

Const FmtStandard As String = “Standard”

The statement to display (net) cash can then be changed to the following:

txtNetCash.Text = Format (Cash, FmtStandard)

All other amounts, including the two text boxes, can be formatted in a similar fashion. In case you decide to change the format later, such as to Currency, all you will have to do is to change the constant value. Notice that because FmtStandard is a constant name, it should not be enclosed in a pair of double quotes when placed in the Format function as the second parameter.

Look It Up
You can also use the FormatNumber function to format a number. Use the keyword FormatNumber in the Index tab of the help file for details on how to use the function.
For a complete list of the characters used for the user defined format string for numbers, use the keyword Format function to search the Index tab; then select user-defined numbers to view the table.

A Side Note on Design

In the previous example, the credit card rate was treated as a constant. In a real application, this may not be a good design because the rate can change. Each time the change occurs, you will need to change and recompile the program. A well thought out design should call for the rate to be saved in some file/database, and read in when your program starts. I have seen a commercial program with the social security tax rate hard-coded in the program. When the client company called for an update, the software company charged the client service fees. Don’t you think this practice is taking advantage of their poor design?

Last change: February 13 2016 18:47:35.