I have this little tutorial program I am playing with in C++ with QT. It converts 2 currencies at once based on user input. And it keeps all three of them synced up, by monitoring the change in any one value and changing the other 2. It works now, but only if I give it conversion rates that can be successfully divided and then multiplied back to equal the exact same result. Because a change in one value will trigger the program to change the others. So the math has to work out on all of the equations.
The program was supposed to convert Integer to Hex to Binary which will always work out perfectly. I wanted to try to switch it to do currencies.
You can see in the code below. I set Dollar Euro at 1:2 so everything works perfect. If I change the dollar to Euro Conversion to 1.1 or to 3.0 the program will start behaving strange then crash, I am guessing this is because
((1 / 3)*3) is .999999 and = ((1/2)*2) is 1.0
Can anyone point me in the right direction to correct this?
A copy of my class implementation is below
Sorry there is a lot of wonky stuff in QT that is not really pure C++. I tried posting this on QT forums but the spam filter kept kicking it out probably because I mentioned currencies a bunch of times.
Decide how accurate you want your conversions to be, and make the basic unit of your currency accordingly.
For example, you might decide that your basic unit is 1 millionth of a penny. You work entirely in int values, where "1" means "one millionth". You convert it back to whole pennies only for the purpose of displaying to the user.