Home     Services     Tutorials     Pricing     Portfolio     Testimonials     Links     Contact    

 Tutorials
 Dreamweaver ASP
Insert, update and delete records in multiple tables from one form
Delete multiple records
Update multiple records

 ASP - VBScript
Format numeric values
Format Currency values
Working with dates and times

 ASP - Access
Retrieve Record Identity from an autonumber field on insert
Setting up an OLE DB connection string

 ASP - SQL Server
Retrieve record identity from an auto incremented field on insert
Generate a random number
Setting up an OLE DB connection string

Formatting numeric valuesFormatting numeric values

Formatting numeric values

ASP / VBScript
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
 Original value
 Number of digits after the decimal
 Include Leading Digit 0 (default)    -1   -2
 Use Parens For Negative Numbers    0 (default)    -1   -2
 Group Digits 0 (default)    -1   -2
 


 The code generated by the form:  FormatNumber(123.456, 2, 0, 0, 0)
 The formatted value: 123.46


NOTE: If you wish to use the default value (0) you can omit the parameter completely, for example: FormatNumber(123.456, 2)