How to Handle Currencies

WEBINAR: On-demand webcast

How to Boost Database Development Productivity on Linux, Docker, and Kubernetes with Microsoft SQL Server 2017 REGISTER >

During a recent project, I needed to deal with different currencies. To cope with this requirement, I developed a mechanism for handling different currencies; I would like to share this with you.

The Currency Class

The basic element of the currency handling mechanism is a class called Currency. A currency instance consists of Notes, Coins, and a currency letter. An example is 90.5 $ in which there are 90 Notes, 50 Coins, and the currency letter is $. The Currency class contains these three data members.

The functions of the Currency class include four different constructors and simple functions for setting and obtaining the above-mentioned data values. The values can be provided in the form of notes and coins or in the form of float/double values. Then, there are functions for performing operations with currencies, such as addition, subtraction, multiplication, division, obtaining percentages, and making comparisons. Overloaded operators are used for these operations for ease of use. A Boolean data member and its associated function; it which holds and provides (respectively) information about the last operation—whether that operation was carried out successfully or some error occurred. Another function that provides string form of currency exists.

Then, there are a few static members of this class. They are very important and are used to carry out conversions of currencies.

Other Elements of the Mechanism

Following are other elements of the mechanism that are defined in Currency.h file.

  • There is an enumerated data, cname, that defines the names of all the countries whose currencies are being used.
  • There is a constant array of cname, called COUNTRIES, that consists of all the countries defined in cname.
  • There is a constant array of strings called CURR_LETTERS; it consists of currency letters of currencies of all countries defined in cname.
  • There is a constant variable of cname called DEFAULT_COUNTRY. The default country is used for currency conversion rates (as discussed in the following point).
  • There is a constant array of float/double, called BASE_RATES. It consists of conversion rates of the currency of the default country to currencies of all other countries.
  • There is a constant variable of short integer, called MAX_COUNTIRES, that holds the total number of countries/currencies being used.

How the Mechanism Works

The values of cname, COUNTRIES, CURR_LETTERS, and BASE_RATES are defined in the same sequence. It means that, if the first value in cname is America, the first value of COUNTRIES will also be America, the first value of CURR_LETTERS will be $, and the first value of BASE_RATES will be the conversion rate required to convert the currency of the default country into dollars. This symmetry of sequence allows the mechanism to work easily using respective array indices.

The Currency class has a static data member, called currency_rates; it is a two-dimensional array used to hold the currency conversion rates of all currencies. A static member function called init_currency_rates is used to initialize the values of currency_rates. This function must be called at program startup, so that the values of currency_rates can be initialized at startup and used later on. This function uses the constant array BASE_RATES to determine the conversion rates of currencies of all countries to currencies of all other countries.

The Currency class has two versions of a static function, called convert_currency, that are used to convert an amount of one currency into another currency. Once the currency conversion rates are initialized at startup, any of these functions can be called to convert amounts in different currencies.

Adding More Countries and Currencies

There are currently eight countries/currencies used in the sample project. Adding more countries in the mechanism is fairly simple. You have to amend in following five places, which all exist in the Currency.h file:

  1. Add the required country name in the definition of cname.
  2. Add the required country name in the COUNTRIES array in same place as in cname.
  3. Add the currency letter of the required country in the CURR_LETTERS array in same place as in cname.
  4. Add the conversion rate of the currency of the default country into the currency of required country in the BASE_RATES array in same place as in cname.
  5. Increment the constant MAX_COUNTRIES to reflect change in all areas.

Some Conventions Used and Limitations

The following conventions are used in creating the Currency class; they were suitable for my project. The changes can be made easily according to any particular functionality.

  • The notes and coins system is used. For notes, the ULONG data type is used; it can only contain values ranges from 0 to 4,294,967,295. If you require holding larger values, you can use __int64 or ULONGLONG. If even larger values are required, you can use double numbers instead of the notes and coins system. In that case, the data members and respective functions will be changed, but the mechanism will remain the same.
  • Negative currencies are not allowed. Hence, the ULONG data type is used for notes (instead of LONG).
  • Because negative currencies are not allowed, subtraction of a higher currency amount from lower currency amount is not allowed.
  • Multiplications, divisions, and percentages are allowed only with numbers other than currencies. This means you can multiply 526.65 $ with the number 5.6, but not with 5.6 $.
  • In comparison, the deviation of plus-minus 1 in coins is kept acceptable. It means 1.5 $ is equal to both 1.4 $ and 1.6 $ (also 1.5 $, of course).
  • The currency letters are added in urgency and carelessly; for example, E is written for Euro. The same can be resolved by adding the proper currency letters of the currencies.

The Sample Project

To demonstrate the use of the currency mechanism, a simple Currency Calculator is used. You can use this currency calculator to convert amounts in different currencies and to add, subtract, multiply, and divide the currencies. The calculator is fairly simple and was just created to demonstrate some of the functionalities of the subject.

About the Author

Anwar-ul- Haque

An Electrical Engineer working in BOC Pakistan as a Supply Chain Manager. Computer Programming is my hobby and I write programs in my spare times.



  • computer langueges.

    Posted by mrs. ashoo deswal on 04/11/2014 12:57am

    I would like to learn computer languages like oracle etc.

  • Looks Good but

    Posted by alan93 on 10/30/2007 03:25pm

    I would have prefered a better summary at beginning rather than a line saying "deals with currency" so I would have known what "deal with" meant without having to read most of article. Also, it would have been great to see a percent key on the calculator because that is where I have run into rounding problems when working with currency.

  • Don't you know about GetLocaleInfo API and MFC COleCurrency class?

    Posted by VictorN on 10/05/2007 09:10am

    I am awfully sorry, Anwar, but you seem to reinvent the wheel: the most of the methods you implemented in your Currency class already exist in the MFC class COleCurrency! (Note, your Currency class is already using MFC!). So, the only "new" feature I see in this class is converting from one currency to another. But where is the method to update the currency rates? The article would be more better and useful if you implemented this Currency class as a wrapper for Window APIs used to get the currency information, such as GetLocaleInfo. With the best regards and waiting for the total update, Victor

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • In this era of Big Data, enterprise applications create a large volume of data that may be structured, semi-structured, or unstructured in nature. Application development cycles are also shorter and application availability is a critical requirement. As such, enterprises are forced to look beyond traditional relational databases to NoSQL databases, such as MongoDB and Apache Cassandra, to onboard these next-generation applications. This guide discusses the backup and recovery challenges for distributed and …

  • Chuze Fitness is a fast-growing fitness chain with over 21 locations spanning California, Arizona and Colorado. Chief information and marketing officer, Kris Peterson, explains why access to fast and reliable Wi-Fi is a "must have" service at their gyms and why they switched to Ruckus Cloud Wi-Fi. Chuze Fitness needed to provide a good user experience to the hundreds of guests streaming music, podcasts and videos as they worked out. They also needed to adequately cover their sprawling 20-40,000 square foot …

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date