FormatMoney(…)

Description

Returns a formatted String version of the supplied amount value, according to the specified formatting parameters.

All aspects of the formatting are configurable so that monetary values can be displayed in just about any currency, whether it be pounds and pence, dollars and cents, deutschmarks and pfennigs, Japanese yen or Korean won. Currency symbol prefixes and suffixes, negative values and thousands separators are all supported. See the working example below for a demonstration.

Syntax

strMoney = FormatMoney( amount, currency_symbol_before, currency_symbol_after, thousands_separator, decimal_point, significant_after_decimal_pt, display_after_decimal_pt )

Parameters

amountThe value to be formatted. Usually amount is a numeric value but a String can be supplied, since parseFloat(…) is used to check the incoming value.
currency_symbol_beforeCurrency symbol/text string to be placed before the amount (and after any − sign)
currency_symbol_afterCurrency symbol/text string appended to the amount.
thousands_separatorThousands separator string to apply for amounts ≥ 1000.
decimal_pointDecimal point (period) string to apply if required.
significant_after_decimal_ptNumber of significant digits after the decimal point.
display_after_decimal_ptNumber of digits to display after the decimal point. (Must be ≥ 0)

Return Value

Returns a String containing the formatted amount, or a String with the digits replaced by hash marks (#) if the formatted value contained too many digits.

Just about any currency can be easily supported by changing the appropriate parameters. By pressing the Calculate button in the example below you'll get a good idea of how these parameters affect the outcome.

 

Working Example

The following example shows how the general FormatMoney() JavaScript routine can be used to display monetary amounts in a variety of different currencies and formats.

Price per kg/itemQuantity (kg/items)Actual value

Results when following parameter values are applied to FormatMoney() function

currency symbol beforecurrency symbol afterthousands separatordecimal pointsignificant after decimal ptdisplay after decimal ptFormatMoney()

JavaScript Source

You may download FormatMoney.js for use in your own projects; please read the licensing terms below.

Links

The following are links to external sites discovered during Google searches:

Licensing

This document and the software contained herein are covered by the MIT License; see the license page for more information.

Valid HTML 4.01!