FormatCurrency - VBScript


Overview

The FormatCurrency command in VBScript is utilized to format a numeric value as a currency string. It converts the value into a localized currency representation, incorporating currency symbols, decimal separators, and grouping separators. This command is commonly used for displaying monetary values in a readable and familiar format.

Syntax

FormatCurrency(Number [,NumDigitsAfterDecimal] [,IncludeLeadingDigit] [,UseParensForNegativeNumbers] [,GroupSeparator] [,DecimalSeparator] [,CurrencySymbol])

Options/Flags

  • NumDigitsAfterDecimal: Sets the number of decimal places displayed. Defaults to 2.
  • IncludeLeadingDigit: Adds a leading digit before the decimal point if the value is less than 1. Defaults to True.
  • UseParensForNegativeNumbers: Encloses negative values in parentheses. Defaults to False.
  • GroupSeparator: Specifies the character used to separate groups of digits. Defaults to , (comma).
  • DecimalSeparator: Specifies the decimal separator character. Defaults to . (period).
  • CurrencySymbol: Sets the currency symbol to use. Defaults to the system’s default currency.

Examples

Simple usage:

FormatCurrency(123.45) ' Returns "$123.45"

Custom formatting:

FormatCurrency(12345.6789, 4, False, True, " ", "-", "€") ' Returns "€ 12,345.6789"

Negative number in parentheses:

FormatCurrency(-123.45, 2, False, True) ' Returns "($123.45)"

Common Issues

  • Ensure the input value is a numeric type.
  • The CurrencySymbol parameter must be a valid currency symbol recognized by the system.
  • Use the IncludeLeadingDigit option to avoid misleading representations of values less than 1.

Integration

The FormatCurrency command can be combined with other VBScript commands for advanced tasks:

  • Currency Conversion: Use the Round function to perform currency conversion with FormatCurrency.
  • Data Formatting: Integrate FormatCurrency into InputBox or MsgBox functions to display currency values in user interfaces.
  • FormatNumber
  • FormatPercent
  • Round