Highlights
All Snippets
Top 100 Snippets
Librarians
gbCodeLib

By Language
VB6
JavaScript
Perl
HTML
SQL
Java
DOS

GBIC >> Source Code >> Visual Basic >> Snippet

Using format$


If you Try To Format a number without specifying Format
Format provides functionality similar To the Str Function
positive numbers formatted As strings using Format Do Not include a leading Space

StringResult = Format (expression, Format )

'Named Date/Time Formats
'General Date Display a Date and/or Time. 4/3/93 05:34 PM  4/3/93  05:34 PM
'Long Date Display a Date according to your system's Long Date Format. 
'Medium Date Display a Date using the medium Date Format  
'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 AM/PM designator. 
'Short Time Display a Time using the 24-Hour Format, For example, 17:45. 

'Named Numeric Formats
'General Number Display number with no thousand separator. 
'Currency Display number with thousand separator, If appropriate; display two digits to the right of the decimal separator. output Is based on system locale <JavaScript:alink_4.Click()> settings. 
'Fixed  Display at least one digit to the left and two digits to the right of the decimal separator. 
'Standard Display number with thousand separator, 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. 
'Scientific Use standard scientific notation. 
'Yes/No Display No if number is 0; otherwise, display Yes. 
'True/False Display False if number is 0; otherwise, display True. 
'On/Off Display Off if number is 0; otherwise, display On. 

'User-Defined Numeric Formats
'None Display the number with no formatting. 
'(0) Digit placeholder. Display a digit or a zero. Display leading zero(s) as needed.  Round to right as many decimal places as there are zeros
'(#) Digit placeholder. Display a digit or Nothing. Display nothing In an empty position.  Leading and trailing zeros aren't displayed
'(.) Decimal placeholder.
'(%) Percentage placeholder. The expression is multiplied by 100. The percent character (%) Is inserted in the position where it appears in the Format String. 
'(,) Thousand separator.
'(:) Time separator. separates hours, minutes, and seconds 
'(/) Date separator separates the Day, Month, and Year 
'- + $ ( ) Display a literal character. To display a character other than one of those listed, precede it with a backslash (\) or enclose it In Double quotation marks (" "). 
'(\) Display the next character in the Format String. To display a character that has special meaning as a literal character, precede it With a backslash (\). The backslash itself isn't displayed. Using a backslash Is the same As enclosing the Next character In Double quotation marks. To display a backslash, use two backslashes (\\).  Examples of characters that can't be displayed As literal characters are the Date-formatting And Time-formatting characters (a, c, d, h, m, n, p, q, s, t, w, y, / And :), the numeric-formatting characters (#, 0, %, E, e, comma, and period), and the string-formatting characters (@, &, <, >, and !). 
'("ABC") Display the string inside the double quotation marks (" "). to include a string in Format from within code, you must use Chr(34)

Format (8315.4, "00000.00" )   08315.40
Format (8315.4, "#####.##" )   8315.4
Format (8315.4, "##,##0.00" )   8,315.40
Format (315.4, "$##0.00" )    $315.40
Format (Now, "m/d/yy" )    1 / 27 / 93
Format (Now, "dddd, mmmm dd, yyyy" )  Wednesday, January 27, 1993
Format (Now, "d-mmm" )    27 - Jan
Format (Now, "mmmm-yy" )    January - 93
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

Sweden   Format (Now, "ddddd ttttt" )  1992 - 12 - 31 18.22.38
United Kingdom   Format (Now, "ddddd ttttt" )   31 / 12 / 92 18:22:38
Canada (French)   Format (Now, "ddddd ttttt" )   92 - 12 - 31 18:22:38
United States   Format (Now, "ddddd ttttt" )   12 / 31 / 92 6:22:38 PM