Rounding Decimals

February 14, 2010 at 12:40:26
Specs: Windows Vista
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.

See More: Rounding Decimals

Report •


#1
February 14, 2010 at 16:30:24
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.

Report •

#2
February 14, 2010 at 16:48:08
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) * 100

This is VB but should be easily adapted to C++

Stuart


Report •

#3
February 15, 2010 at 07:27:49
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.


Report •

Related Solutions

#4
February 15, 2010 at 08:19:21
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


Report •


Ask Question