Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 17

Formatting Functions

VISUAL BASIC

What is Formatting Function?


-----Formatting output is a very important part of
programming so that the visual interface can be
presented clearly to the users. Data in the previous
lesson were presented fairly systematically through
the use of commas and some of the functions like Int,
Fix and Round. However, to have better control of
the output format, we can use a number of
formatting functions in Visual basic.

Three Common Formatting Functions in


VB

Tab
Space
Format

The Tab Function


The synatx is Tab (n); x
The item x will be displayed at a position that is n

spaces from the left border of the output form. There


must be a semicolon in between Tab and the items
you intend to display (VB will actually do it for you
automatically).

Example:
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles Button1.Click
MessageBox.Show("The Three common Formatting
Functions in VB are:" & vbNewLine & vbTab & "- The Tab
Function" & vbNewLine & vbTab & "- The Space Function"
& vbNewLine & vbTab & "- The Format Function ")
End Sub
End Class

Fig.1

The Space Function


The Space function is very closely linked to the Tab

function. However, there is a minor difference. While


Tab (n) means the item is placed n spaces from the
left border of the screen, the Space function specifies
the number of spaces between two consecutive
items. For example, the procedure

Example
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles Button1.Click
MessageBox.Show("The Three Common Formatting
Functions in VB are:" & vbNewLine & "First:" & Space(5) &
"-The tab Function" & vbNewLine & "Second:" & Space(10)
& "-The Space Function" & vbNewLine & "Third" &
Space(10) & "- Format Function")
End Sub
End Class

Fig2

The Format Function


The Format function is a very powerful formatting
function which can display the numeric values in
various forms. There are two types of Format
function, one of them is the built-in or predefined
format while another one can be defined by the users.
Syntax:
Format (n, style argument)
where n is a number and the list of style arguments is
given in the table

Style argument
General Number

Fixed

Standard

Currency

Percent

Explanation
To display the number without
having separators between
thousands.
To display the number without
having separators between thousands
and rounds it up to two decimal
places.
To display the number with
separators or separators between
thousands and rounds it up to two
decimal places.
To display the number with the dollar
sign in front, has separators between
thousands as well as rounding it up
to two decimal places.
Converts the number to the
percentage form and displays a %
sign and rounds it up to two decimal
places.

Example
Format(8972.234, General
Number)=8972.234
Format(8972.2, Fixed)=8972.23

Format(6648972.265, Standard)=
6,648,972.27

Format(6648972.265, Currency)=
$6,648,972.27

Format(0.56324, Percent)=56.32 %

Example:
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles Button1.Click
MessageBox.Show(Format(TextBox1.Text, "general
number") & vbNewLine & Format(TextBox1.Text, "Fixed")
& vbNewLine & Format(TextBox1.Text, "Standard") &
vbNewLine & Format(TextBox1.Text, "Currency") &
vbNewLine & Format(TextBox1.Text, "Percent"))
End Sub
End Class

Fig3

The syntax of the user-defined Format function is


Format (n, users format)
Although it is known as user-defined format,

we still need to follows certain formatting


styles. Examples of user-defined formatting
style are listed in Table next slide

Example

Format(781234.57,0)

Explanation

Rounds to whole number


without separators between
thousands.
Format(781234.57,0.0)
Rounds to 1 decimal place
without separators between
thousands.
Format(781234.576,0.00)
Rounds to 2 decimal places
without separators between
thousands.
Format(781234.576,#,##0.00) Rounds to 2 decimal places with
separators between thousands.
Format(781234.576,$#,##0.00 Shows dollar sign and rounds to
)
2 decimal places with separators
between thousands.
Format(0.576,0%)
Converts to percentage form
without decimal places.
Format(0.5768,0.00%)
Converts to percentage form
with 2 decimal places.

Output

781235

781234.6

781234.58

781,234.58
$781,234.58

58%
57.68%

Example:
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles Button1.Click
MessageBox.Show(Format(Val(TextBox1.Text), "0") &
vbNewLine & Format(Val(TextBox1.Text), "0.0") & vbNewLine &
Format(Val(TextBox1.Text), "0.00") & vbNewLine &
Format(Val(TextBox1.Text), "#,##0.00") & vbNewLine &
Format(Val(TextBox1.Text), "$#,##0.00") & vbNewLine &
Format(Val(TextBox1.Text), "0%") & vbNewLine &
Format(Val(TextBox1.Text), "0.00%"))
End Sub
End Class

Fig4

You might also like