Search Tools Links Login

VB6 Tutorial 35: Formatting Functions


This lesson will take you through the formatting functions in Visual Basic 6 which you can use to format numeric or string expressions.

You have probably seen in many applications that you enter a number and that number is displayed in a different format for a better readability. Formatting functions are useful when you want to represent an expression in many different ways. For instance, you may want to convert an expression to currency format, phone number format, credit card number format or percentage format. Sometimes its necessary to represent the date-time values in a couple of ways. Visual Basic 6.0 has included many powerful functions in order to achieve this.

FormatNumber

The FormatNumber function converts an expression to the numeric format.

Example

Print FormatNumber(2234.789, 2)

Output: 2234.79

FormatCurrency

The FormatCurrency function converts an expression to the currency format.

Example

Print FormatCurrency(2234.789,2)

Output: $2234.79

FormatPercent

The FormatPercent function converts the expression to the percentage format.

Example

Print FormatPercent(.225, 2)

Output: 22.50 %

Displaying current Date/Time

Before showing you formatting date-time values, lets see how to display date and time. Remember that Time and Date are properties, not functions in the following example.

Private Sub cmdShowDateTime_Click()
     'Displays current time
     Print Time             '12:53:12 PM

     'Displays current date
     Print Date             '04-09-2012

     'Displays current date,time
     Print Now              '04-09-2012  12:53:12 PM
End Sub

FormatDateTime

The FormatDateTime function displays a date/time value in different formats.

Example

Print FormatDateTime("2 - 3 -2012", vbLongDate)
Print FormatDateTime(Now, vbLongDate)
Print FormatDateTime(Time, vbShortTime)    'Displays current time
Print FormatDateTime(Time, vbLongTime)

Output:

02 March 2012
04 September 2012
12:01
12:03:28 PM

Format

The Format function is the most useful among all the formatting functions in the sense that it can be used to perform many formatting tasks. You can use this function to format date-time values, phone number or credit card number, currency and many others.

Example

Print Format(8765, "@@@@@@@")

Output: 8765

Three spaces before 8765.

You can use the Format function to format different types of expressions. The below example shows this.

Example

Print Format("abc", "@@@@@@@@@")            ' abc

Converts into currency form

Print Format(123, "Currency")               ' $123

Converts into uppercase

Print Format("xyz", ">&&&")                 ' XYZ

Converts into lowercase

Print Format("HELLO", "<&&&&&")             ' hello

Converts into phone number or credit card no. form

Print Format(9845678972#, "&&&&-&&-&&&&")   ' 9845-67-8972

Short Time

Print Format(Now, "hh:mm")                  ' 12:38

Print Format(Now, "hh:mm:ss")               ' 12:38:00

Print Format(Now, "hh:mm AMPM")             ' 12:38 PM

Print Format(Date, "dddd mmm dd, yyyy")     ' Tuesday September 04, 2012

Print Format(Date, "dd mmmm")               ' 04 September

About this post

Posted: 2018-04-20
By: vb6boy
Viewed: 3,988 times

Categories

Visual Basic 6

VB6 Tutorial

Attachments

No attachments for this post


Loading Comments ...

Comments

No comments have been added for this post.

You must be logged in to make a comment.