Dr Dev
Telephone01303 766482
E-mail
HomeServicese-CommercePricingTutorialsTestimonialsPortfolioLinksContact
Tutorials
ASP - Dreamweaver
INSERT, UPDATE & DELETE records in multiple tablesDELETE multiple recordsUPDATE multiple records
ASP - VBScript
Formatting numeric valuesFormatting currency valuesWorking with dates and times
ASP - Access
Retrieve a record identity from an Access database autonumber field on INSERT
ASP - SQL Server
Retrieve a record identity from a SQL Server auto-incremented field on INSERTGenerate a random number
ASP - VBScript

Formatting currency values ASP/VBScript

Use the VBScript FormatCurrency function to change the way a value 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.

FormatCurrency(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 optional arguments are omitted, values for omitted arguments are provided by the computer's regional settings. The position of the currency symbol relative to the currency value is determined by the system'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: 'FormatCurrency'

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.

FormatCurrency example code

<%
dblNumber = 123.456
curResult = FormatCurrency(dblNumber, 2)
%>


curResult would contain 123.46


Changing the currency symbol

You can use the VBScript SetLocale function in conjunction with FormatCurrency to change the currency symbol displayed.

SetLocale(LCID)

You can use the Decimal, Hex, or Short String value for the LCID (Locale ID), for example these will all set the Locale to English - United Kingdom:

SetLocale("en-gb")   :    SetLocale(0x0809)    :    SetLocale(2057)

You can find a list of all the LocaleID's in the Windows Script Documentation, or on-line here.


SetLocale example code

<%
dblPrice = 123.456
If IsNumeric(dblPrice) Then
   intLocale = SetLocale(2057)                 ' English - United Kingdom - £
   curPounds = FormatCurrency(dblPrice, 2)     ' curPounds contains £123.46
   intLocale = SetLocale(1033)                 ' English - United States
   curDollars = FormatCurrency(dblPrice, 2)    ' curDollars contains $123.46
   intLocale = SetLocale(1036)                 ' French - France
   curFrancs = FormatCurrency(dblPrice, 2)     ' curFrancs contains 123.46 F
End If
%>



A note about the euro - €

One might expect that using the SetLocale function to set the LocaleID to that of country which now uses the Euro, France, Germany Spain etc. in conjunction with the FormatCurrency function would produce the result in Euros: 123,45 €

However, this isn't always the case, using this method the Euro symbol will only be displayed if the host server is Windows XP or Server 2003. If the server is Windows 2000 the country's pre-Euro currency symbol may be used. Therefore this method can not be relied upon in all cases.

However, we can work around this by manually adding the Euro symbol, purely for display purposes, for example:

<%
dblPrice = 123.456
If IsNumeric(dblPrice) Then
   intLocale = SetLocale(2057)                   ' English - United Kingdom - £
   curPounds = FormatCurrency(dblPrice, 2)       ' curPounds contains £123.46
   intLocale = SetLocale(1033)                   ' English - United States
   curDollars = FormatCurrency(dblPrice, 2)      ' curDollars contains $123.46
   intLocale = SetLocale(1033)                   ' English - United States
   curEuros = FormatNumber(dblPrice, 2) & " €"   ' curEuros contains 123,46 €
End If
%>


NOTE: I have still used the SetLocale function, prior to FormatNumber in order to display the numbers in the national preference for France where a comma is used to indicate the decimal point: 123,46 €


Demo
 Original value
 Number of digits after the decimal
 Include Leading Digit
      
 Use Parens For Negative Numbers          
 Group Digits       
 Select Locale
 Use Euro Symbol     
 
 The code generated by the form:
 
intLocale = SetLocale(2057)
FormatCurrency(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: FormatCurrency(123.456, 2)
HomeServicese-CommercePricingTutorialsTestimonialsPortfolioLinksContact
Services
Web site design
Database development
Server side scripting
e-Commerce integration
Shopping cart development
Databases
MySQL
Microsoft SQL Server
Microsoft Access

Scripting Languages
PHP
ASP VBScript
JavaScript
Web Technologies
AJAX
DHTML
CSS
Contact
Telephone01303 766482
E-mail