Formatting Numbers, Dates and Times

The Label control is still the easiest way of displaying the result of calculations. Whatever the answer is, just move it to Label5.Caption and it will appear on the form. Unfortunately, it does not always appear the way you want to see it. No problem if the result is a string but, what if it is a dollar amount or a date of some kind. That will require some formatting of the result before displaying it. We use the Format function:
Label5.Caption = Format(result, "formatting characters")

Numbers

For example, given that:

Dim result As Single
result = 3456.7
Label5.Caption = Format(result, "00000.00") 'displays: 03456.70
Label5.Caption = Format(result, "#####.##") 'displays: 3456.7
Label5.Caption = Format(result, "##,##0.00") 'displays: 3,456.70
Label5.Caption = Format(result, "$##,##0.00") 'displays: $3,456.70

Here is a list of what the formatting characters mean:

0
-> represents a digit, with non-significant leading and trailing zeros
#
-> represents a digit, without non-significant leading and trailing zeros
.
-> decimal placeholder
,
-> thousands separator
$ + - ( ) space
-> literal character; displayed as typed

When displaying dollar amounts, it is good practice to always use the 0 placeholder with the decimal so that 10 cents does not come out as $.1 or $0.1 Using the formatting string "$#0.00" ensures that the value follows standard rules and comes out as $0.10.

Dates and Times

When working with dates and times, you need to know that there is a function that will obtain the current date and time from the system clock. The function is: Now( ) and you can use it directly as in:
Label5.Caption = Now( )

The result is the current date and time formatted according to what you specified in the Windows Control Panel for your system. If you want to format the result, because you don't want to see the time, for example, there are formatting characters for date and time, as there are for numbers. The main characters are:

yy -> year without the century - eg: 00
yyyy -> year with century - eg: 2000
m -> month number - eg: 12
mmm -> short name of month - eg: dec
mmmm -> long name of month - eg: december
d -> day of the month, without zero - eg: 8
dd
-> day of the month, with zero - eg: 08
dddd
-> name of the day of the week - eg: Monday
h
-> hour, without zero - eg: 7
hh
-> hour, with zero - eg: 07
mm
-> minutes - eg: 45
ss
-> seconds - eg: 55

Thus, if Now( ) is July 8, 2000 ,
Label5.Caption = Format(Now( ), "dddd, yyyy mmmm dd")
returns: Saturday, 2000 July 08

Of course any other date can be formatted for display:
Label5.Caption = Format( DateOfBirth, "yyyy-mm-dd")

Named Formats

In addition to the formatting string, there are several named formats that can be used to determine the output format. These named formats are VB constants that you call when you need them:

General Number
-> Number with no thousands separator
Currency
-> Thousands separator, two digits to the right of decimal
Fixed
-> Displays at least one digit to the left and two digits to the right of decimal
Standard
-> Thousands separator, at least one digit to the left and two digits to the right of decimal
Percent
-> Multiplies by 100, add percent sign to the right
General Date
-> Display determined by Control panel settings; displays date and time
Long Date
-> Long date format specified for system
Short Date
-> Short date format specified for system
Long Time
-> Long time setting specified by system; includes hours, minutes, seconds
Short Time
-> Shows hours and minutes

Dim DateHired As Date
DateHired = "1995-10-25"
Label5.Caption = Format(DateHired, "Long Date")

returns: October 25, 1995

No comments: