 
Formatting numeric values |
|
Use the VBScript FormatNumber function to change the way a number stored in either
a database field or a variable is displayed.
Here is an extract from the Microsoft Windows Script 5.6 Documentation:
Visit the Microsoft
Windows
Script 5.6 Documentation download
page.
FormatNumber(Expression [,NumDigitsAfterDecimal
[,IncludeLeadingDigit_
[,UseParensForNegativeNumbers [,GroupDigits]]]])
Expression
Required. Expression to be formatted.
NumDigitsAfterDecimal
Optional. Numeric value indicating how many places to the right of the decimal
are displayed. Default value is -1, which indicates that the computer's regional
settings are used.
IncludeLeadingDigit
Optional. Tristate constant that indicates whether or not a leading zero is displayed
for fractional values. See Settings section for values.
UseParensForNegativeNumbers
Optional. Tristate constant that indicates whether or not to place negative values
within parentheses. See Settings section for values.
GroupDigits
Optional. Tristate constant that indicates whether or not numbers are grouped
using the group delimiter specified in the control panel. See Settings section
for values.
The IncludeLeadingDigit, UseParensForNegativeNumbers, and GroupDigits arguments
have the following settings:
| Constant |
Value |
Description |
| TristateTrue |
-1 |
True |
| TristateFalse |
0 |
False |
| TristateUseDefault |
-2 |
Use the setting from the computer's regional settings. |
Remarks
When one or more of the optional arguments are omitted, the values for omitted
arguments are provided by the computer's regional settings.
NOTE: The Expression must be a numeric value,
if you try and format a text or null value you will receive the following error
message:
Microsoft VBScript runtime (0x800A000D) Type mismatch: 'FormatNumber'
You can guard against getting an error by checking that the content of a variable is numeric before you try to format it, using the IsNumeric function, as shown in the examples.
Example code
<%
dblNumber = 123.456
If IsNumeric(dblNumber) Then
dblNumber = 123.456
dblResult = FormatNumber(dblNumber, 2
End If
%>
dblResult would contain 123.46
Demo
NOTE: If you wish to use the default value (0)
you can omit the parameter completely, for example: FormatNumber(123.456,
2) |