|
Manipulating Strings and Integers
There are
many small things that can be done to data types (integers, strings...) in
VB that make the life of the programmer much easier. In this
section, I'll discuss the most important and the most widely used
techniques for manipulating numbers and characters. Here is an index
of what will be discussed in this lesson:
Converting
Data Types
Constants
Dates
and Times
Using
the Format Function
Manipulating
Text Strings
Using
the Format Function The
Format function is a very useful function that basically displays
different variables in date, time and various number formats. The
output, therefore, appears as the programmer wants it to appear, and
problems like a date variable of 12/31/2001 being displayed as 12312001
are quickly and easily resolved. First
of all, here is the syntax for the Format Function:
Format(expression, format style) There
are other optional constants that you can use, but for now, we'll only be
interested in displaying values in a formatted way. In order for the
programmer to achieve desired output, each digit of the value should be
formatted separately. The table below summarizes the different
symbols that can be used for formatting numbers:
| Symbol |
Description |
| 0 |
Digit
placeholder. Prints a trailing or leading zero in
this position |
| # |
Digit
placeholder, doesn't print trailing or leading zero |
| . |
Decimal
Placeholder |
| , |
Thousands
separator |
| -
or _ or $ |
Literal
character. Prints characters exactly as typed |
An example
of a formatted number:
Dim Number1 As Double
Dim Number2 As Double
Dim Result As Double
Number1 = 12.65
Number2 = 4.76
Result = Format(Number1 * Number2, "#.##")
MsgBox Result
When Number1
and Number2 are multiplied together, the result is 60.214,
however, the Format function displays the result as 60.21. Here's
a second example:
Dim Number1 As Double
Dim Number2 As Double
Dim Result As String
Number1 = 2979797.97
Number2 = 1.4
Result = Format(Number1 * Number2, "#,###,###.00")
MsgBox Result Do
you see the difference between this example and the previous one?
This time we're using comma's as thousands separators.
Therefore, our Result is declared as a string. The
result of 4171717.158 is formatted and displayed as 4,171,717.16. The
following tables, which are from Microsoft, show in detail how to apply
formatting to dates, times and other things. The examples assume
that Regional Settings in the Regional Settings Properties dialog box of
Windows Control Panel is set to English(United States).
| Format
Syntax |
Result |
| Format(Now,
“m/d/yy”) |
1/27/99 |
| Format(Now,
“dddd, mmmm dd, yyyy”) |
Wednesday,
January 27, 1999 |
| Format(Now,
“d-mmm”) |
27-Jan |
| Format(Now,
“mmmm-yy”) |
January-99 |
| Format(Now,
“hh:mm AM/PM”) |
07:18
AM |
| Format(Now,
“h:mm:ss a/p”) |
7:18:00
a |
| Format(Now,
“d-mmmm h:mm”) |
3-January
7:18 |
There is
also something called Named Formatting. This is yet an easier
way of formatting variables. All you do is enter standard commands
and everything else is done for you, automatically. For example:
MsgBox Format(80000, "Currency") displays:
$80,000.00
| Format |
Description |
| General
Number |
Display
number as is, with no thousand separators. |
| Currency |
Display
number with thousand separator, if appropriate; display two digits
to the right of the decimal separator. Note that output is based
on system locale settings. |
| Fixed |
Display
at least one digit to the left and two digits to the right of the
decimal separator. |
| Percent |
Display
number multiplied by 100 with a percent sign (%) appended to the
right; always display two digits to the right of the decimal
separator. |
| General
Date |
Display
a date and/or time. For real numbers, display a date and
time (for example, 4/3/93 05:34 P.M.); if there is no fractional
part, display only a date (for example, 4/3/93); if there is no
integer part, display time only (for example, 05:34 P.M.). Date
display is determined by your system settings. |
| Long
Date |
Display
a date according to your system’s long date format. |
| Medium
Date |
Display
a date using the medium date format appropriate for the language
version of Visual Basic. |
| Short
Date |
Display
a date using your system’s short date format. |
| Long
Time |
Display
a time using your system’s long time format: includes hours,
minutes, seconds. |
| Medium
Time |
Display
time in 12-hour format using hours and minutes and the A.M./P.M.
designator. |
| Short
Time |
Display
a time using the 24-hour format (for example, 17:45). |
| Yes/No |
Any
nonzero numeric value (usually – 1) is Yes. Zero is No. |
| True/False |
Any
nonzero numeric value (usually – 1) is True. Zero is False. |
| On/Off |
Any
nonzero numeric value (usually – 1) is On. Zero is Off. |
Make sure you enclose
each of the above Formatting Names in double quotes.
{Go Back To VB Lessons Page}
|