 
Formatting currency values |
|
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
NOTE: If you wish to use the default value (0)
you can omit the parameter completely, for example: FormatCurrency(123.456,
2) |