Written By TheVBProgramer.
$ReqTestHarness$
In addition to the named numeric formats described previously, you can also create custom numeric formats using a combination of specific characters recognized by the Format$ function. To format numbers, combinations of the following characters can be used:
Format Character |
Description |
0 Digit placeholder |
Display a digit or a zero. If the expression has a digit in the position where the 0 appears in the format string, display it; otherwise, display a zero in that position. If the number has fewer digits than there are zeros (on either side of the decimal) in the format expression, display leading or trailing zeros. If the number has more digits to the right of the decimal separator than there are zeros to the right of the decimal separator in the format expression, round the number to as many decimal places as there are zeros. If the number has more digits to the left of the decimal separator than there are zeros to the left of the decimal separator in the format expression, display the extra digits without modification. |
# Digit placeholder |
Display a digit or nothing. If the expression has a digit in the position where the # appears in the format string, display it; otherwise, display nothing in that position. This symbol works like the 0 digit placeholder, except that leading and trailing zeros aren't displayed if the number has the same or fewer digits than there are # characters on either side of the decimal separator in the format expression. |
. Decimal placeholder |
In some locales, a comma is used as the decimal separator. The decimal placeholder determines how many digits are displayed to the left and right of the decimal separator. If the format expression contains only number signs to the left of this symbol, numbers smaller than 1 begin with a decimal separator. If you always want a leading zero displayed with fractional numbers, use 0 as the first digit placeholder to the left of the decimal separator instead. The actual character used as a decimal placeholder in the formatted output depends on the Number Format recognized by your system. |
% Percent placeholder |
The expression is multiplied by 100. The percent character (%) is inserted in the position where it appears in the format string. |
, Thousand separator |
In some locales, a period is used as a thousand separator. The thousand separator separates thousands from hundreds within a number that has four or more places to the left of the decimal separator. Standard use of the thousand separator is specified if the format contains a thousand separator surrounded by digit placeholders (0 or #). Two adjacent thousand separators or a thousand separator immediately to the left of the decimal separator (whether or not a decimal is specified) means "scale the number by dividing it by 1000, rounding as needed." You can scale large numbers using this technique. For example, you can use the format string "##0,," to represent 100 million as 100. Numbers smaller than 1 million are displayed as 0. Two adjacent thousand separators in any position other than immediately to the left of the decimal separator are treated simply as specifying the use of a thousand separator. The actual character used as the thousand separator in the formatted output depends on the Number Format recognized by your system. |
- + $ ( ) space |
Displays the character as is, in the position specified by the format string. To display a character other than one of those listed, precede it with a backslash (\). |
E- E+ e- e+ Scientific format |
If the format expression contains at least one digit placeholder (0 or #) to the right of E-, E+, e-, or e+, the number is displayed in scientific format and E or e is inserted between the number and its exponent. The number of digit placeholders to the right determines the number of digits in the exponent. Use E- or e- to place a minus sign next to negative exponents. Use E+ or e+ to place a minus sign next to negative exponents and a plus sign next to positive exponents. |
To demonstrate custom numeric formats using combinations of the characters listed above, set up another "Try It" project, and place the following code in the cmdTryIt_Click event:
Private Sub cmdTryIt_Click()
Dim strUserInput As String
Dim dblTestNumber As Double
strUserInput = InputBox("Please enter a number to demonstrate named numeric formats:")
dblTestNumber = Val(strUserInput)
Print "Using '00000.00':"; Tab(25); Format$(dblTestNumber, "00000.00")
Print "Using '#####.##':"; Tab(25); Format$(dblTestNumber, "#####.##")
Print "Using '##,##0.00':"; Tab(25); Format$(dblTestNumber, "##,##0.00")
Print "Using '$##0.00':"; Tab(25); Format$(dblTestNumber, "$##0.00")
Print "Using '0.00%':"; Tab(25); Format$(dblTestNumber, "0.00%")
End Sub
Run the project and click the "Try It" button. When the input box comes up, enter 1234.5. The output will be displayed as shown below:
Test the code using a variety of different values, including negative numbers and zero.
Download the VB project code for the example above here.
formating number
pleaaase, help me, how to format a numbers like this:
with vb6 or vba
-25.548
27.575
789.694
1.247
computer
your blog post are nice!
Wish you make more so people will find their needs better AND FASTER :D
Data Format
Dear Sir,
I am new in VB6.0.
If the values are as like 6.8235 or 6.623546.
So i want to remove decimal values and should displays only 7
If the values are as like 6.23548 or 6.4589653.
So i want to remove decimal values and should displays only 6
just use "#"
just use "#"
informatics partices
simple and short but not useful example.
I have a problem with decimal
I have a problem with decimal points. I'm newish to VB6.
in a test box i am putting in the numeric value "0.2" or similar. When i put in "0..2" i dont get any error and the final calculated value is 0 as it my program sees the 0..2 as being 0.
How do i handle this error so I can get a msgbox to say that there is an error in the this testbox?
suppress decimal point?
How do I format a number to show
2 if the number is 2.0
but 2.1 if the number is 2.1
i.e. only show the decimal point if the number is not a whole number and only show it to 1 decimal place?
Format
Use "#.#"
Add value with separators
I want to know how to add two values with separators (##,##0.00)
FEEDBACK
All the topics in the tutorial are excellent and of much use!!!!!Looking forward to more like these.......
right justify
Hey,
The simpliest way to right justify it is:
Space(12 - Len(Format( someValue , "#,###,##0.00"))) & Format(someValue, "#,###,##0.00")
Now, you should know that in true type fonts not all the numbers/letters are of the same width, so you have to choose a font that has the same width for all numbers, for example : Courier New (this is possible with the Printer Object too)
A little late, but just entered here, hope it helps somebody.
Bye!
Renzo Mendoza.
the easiest way to
the easiest way to right-justify is formar$("whatever","@@@@@@@@@@@@@@")
How to print number with right aligment
for eg..
I want to print the following format , I am using printer.print command.
ie., value print in each line should be rightjustify method
' 2345.67
' 1234.67
' 123.45
' 12.34
' 1.23
' 0.12
please help me for how to print this method through printer.print method.