I am writing a program to figure how much change you have and break the answer into dollars and cents. I am having trouble with the dollar amount because using decimal i have a .98 or whatever and i need to know how to eliminate the factors after the decimal and keep the numbers before it. any help would be appreciated.I have the rest of the program figured out except for this.

You have money - bills and coins. Count the number of each

item and multiply my the value of each item and add. Total

dollars is the integral value of the total, cents is the balance.

I think I'm reading the question differently than Wizard-Fred. If you want to seperate the collars and cents into two different number this should do it assuming that C++ had a Mod function.

Dim Amount As Double

Dim Dollars As Integer

Dim Cents As Integer

Amount = 12.96

Dollars = (Int(Amount) Mod Amount)

Cents = (Amount - Dollars) * 100This is VB but should be easily adapted to C++

Stuart

You could of course take the smallest unit - in this case : 1 cent - as your basic unit. So, you express everything in cents, like 1 cent, 2 cents, 50 cents, 100 cents ... and when displaying, you convert cents to dollars. That way, you don't need to use any decimal, nowhere. Of course, you need to see if the max (and min) value is within the range you need.

You could of course take the smallest unit - in this case : 1 cent - as your basic unit.That is in fact how a lot of financial applications work especially those working in the currency market. All the calculations are done using integers. and eliminates any floating point errors inherent in binary arithmetic.

Stuart

Ask Your Question

Weekly Poll

Do you think state sales tax should be charged on Internet purchases?

Discuss in The Lounge

Poll History