Guideline C: Respect for culture and conventions

C7: Monetary amount

Allowing the selection of monetary amount format
As with decimal numbers, different countries present monetary amounts differently. This is in addition to the obvious differences in the use of the country's currency symbol.

Currency symbols
Monetary values are usually accompanied by a currency symbol, which can exist in either the national or international form. The ISO/IEC international standard 4217 specifies the currency symbols used around the world.

Example: In Canada, the national form is the $ symbol, and the international abbreviation is CAD.

Presentation formats
Currency symbols may be placed before, within, or after the radix character of the monetary amount, and there may or may not be a space between the symbol and the amount.

Example: The following list shows several different ways to present the same monetary amount:


US $12,345.67
US USD 12,345.67
Denmark kr 12.345,67
France 12 345,67 €
Portugal 12.345$67 €

Guideline C7


Allow the user to select the monetary amount format.

Example: The following Java program uses the NumberFormat class to present a given monetary amount in a culturally correct format:

import java.text.*;
import java.util.*;
public class GuidelineC7 extends Guideline {
private static final String RESOURCE = "GuidelineeC7";
private double number = 1234567.89;
public String getItRight() {
ResourceBundle bundle = ResourceBundle.getBundle
(RESOURCE, Sample.locale);
NumberFormat cf =
NumberFormat.getCurrencyInstance(Sample.locale);
/* replace Sample.locale with Locale.US
to explicitly specify the English Locale
or with Locale.GERMANY
to explicitly specify the German Locale
*/
return cf.format(number);
}
public String getResource() {
return RESOURCE;
}
}

Depending on the user’s locale preference, the following results would be achieved:


pcMonAmtLocale Result
USA $1,234,567.89-
¤ USD 1,234,567.89-
Germany -€1.234.567,89
¤ -EUR 1.234.567,89
France -1234567,89€
¤ -1234567,89 EUR

Notes:

  1. To minimize rounding loss, store the monetary amount in the C data type long int or long long int, and use integer arithmetic for all calculations.
  2. Respect the monetary amount’s inherent preference instead of the user’s preference. If you respect the user’s preference, the user can alter the currency symbol associated with the amounts, and render the data useless.

Example: Mary has USD 1,000.00 in her bank account in the USA. When Dietrich at the bank's branch in Germany wants to inquire about Mary's current account balance, your product displays EUR 1.000,00!

Presentation field size
For some currencies, the monetary amount can be up to ten thousand times larger than what is needed to express an equivalent amount in the United States dollars. You must factor in two more characters whenever brackets are used to denote a negative amount, and up to 4 characters for the international currency symbol. The field size for monetary amounts can be large for some countries.