Float to int conversion by using two int variables for representation of the float variable

K

k3n3dy

Hello everybody, I would like to ask anybody to help me understand what
should be done exactly with this Problem. I have provided what I have
up to now, although it is not quite accurate.

Create a class to operate on the US currency. Call the new class Money.
All the data components in this class for dollars and cents are integer
variables (this is where I get confused and don't know how to actually
do the job). Include member functions and constructors to initialize
Money to 0 and to initialize it to fixed values. A member function
should display it in appropriate format. Include member functions that
permit data conversion in both directions:
- from real (float) value (4.5f US) to dollars/cents ( 4 dollar, 50
cents)
- from dollars/cents (4 dollar, 50 cents) to real (float) value (4.5f
US)

Here it is what I have managed to do up to now although I am not sure
if I am on the right way even. So if anybody could help me with this I
would really appreciate, it is interesting, but I've been trying for
quite some time and I just can't figure it out!

Thanks to anyone in advance!

#include <iostream>
using namespace std;

class Money {

private: int dollars; int cents;

public: Money() { dollars = 0; cents = 0; }

Money (int dol, int cnt) { dollars = dol; cents = cnt; }

Money (float fl_dollars) // one-arg constructor
{

cout << "fl_dollars value is: " << fl_dollars << endl;

float flt_dollars = fl_dollars;
dollars = int(flt_dollars);
cout << "fl_dollars value is: " << flt_dollars << endl << " And
dollars are: " << levs << endl;
float cents_part = float(fl_dollars - dollars);
cents = cents_part;
cout << "Cents after conversion: " << cents << endl;
cout << "cents value is: " << cents<< endl;
cout << "\n 1-arg Constructor activated\n";
}


void ShowMoney()
{
cout <<"\nDistObject= " << dollars << " dollars" <<" and "<< cents
<< " cents" << endl << endl;
}
};
void main ()
{
// Conversion from Basic to User Defined >>> one-arg Constructor
Money d3 = 10.89;

d3.ShowMoney();

}
 
V

Victor Bazarov

k3n3dy said:
Hello everybody, I would like to ask anybody to help me understand
what should be done exactly with this Problem. I have provided what I
have up to now, although it is not quite accurate.

[..]
class Money {

private: int dollars; int cents;

public: Money() { dollars = 0; cents = 0; }

Money (int dol, int cnt) { dollars = dol; cents = cnt; }

Money (float fl_dollars) // one-arg constructor
{

cout << "fl_dollars value is: " << fl_dollars << endl;

float flt_dollars = fl_dollars;
dollars = int(flt_dollars);
cout << "fl_dollars value is: " << flt_dollars << endl << " And
dollars are: " << levs << endl;

I am guessing 'levs' should be 'dollars' here...
float cents_part = float(fl_dollars - dollars);
cents = cents_part;

If 'cents_part' is 0.89 (the fraction of the dollar amount), how many
full cents is it? How did you get that answer?
cout << "Cents after conversion: " << cents << endl;
cout << "cents value is: " << cents<< endl;
cout << "\n 1-arg Constructor activated\n";
}


void ShowMoney()
{
cout <<"\nDistObject= " << dollars << " dollars" <<" and "<< cents
<< " cents" << endl << endl;
}
};
void main ()
{
// Conversion from Basic to User Defined >>> one-arg Constructor
Money d3 = 10.89;

d3.ShowMoney();

}

V
 
K

k3n3dy

yes my bad it should have been dollars instead of levs, i have
forgotten to change it with dollars.
 
V

Victor Bazarov

k3n3dy said:
yes my bad it should have been dollars instead of levs, i have
forgotten to change it with dollars.

Since it was I who replied to your original post, I know what you're
talking about. However, for anybody else it can be a total mystery.
Please quote relevant portion of the post you're replying to.

And what about other notes of mine? Have you figured it out?

V
 
D

Default User

Victor said:
Since it was I who replied to your original post, I know what you're
talking about. However, for anybody else it can be a total mystery.
Please quote relevant portion of the post you're replying to.

The information below may be of value to k3n3dy or other Google users.



Brian
 
K

k3n3dy

If 'cents_part' is 0.89 (the fraction of the dollar amount), how many
full cents is it? How did you get that answer?

Well, as you can see I am cutting it off by the expression:

float cents_part = float(fl_dollars - dollars);
cents = cents_part;

and then what is left is an integer value that I accept as the value
for the cents. At the same time I am aware that in the way I achieve
this I am restricting the precision only to two signs after the comma
but for the purpose of the program, which is a homework assignment it
is enough.
If 'cents_part' is 0.89 (the fraction of the dollar amount), how many
full cents is it? How did you get that answer?

I think that I don't quite get it! What do you mean by full cents?
Maybe something like the following although for it is one and the same
thing just written in a different form:

cents( (fl_dollars - dollars) * 100 ) or maybe something like this:

cents( static_cast<int>( fl_dollars * 100 ) % 100 )

Best regards,

kenedy
 
V

Victor Bazarov

k3n3dy said:
Well, as you can see I am cutting it off by the expression:

float cents_part = float(fl_dollars - dollars);
cents = cents_part;

and then what is left is an integer value that I accept as the value
for the cents.

OK, let's try it again. 'fl_dollars' is 12.89. 'dollars' is 12. Tnen
'cents_part' is 12.89 - 12.00, or 0.89. Eighty-nine one-hundredths.
That's less than 1. How can you "accept" it as "the value for the cents".
Shouldn't it be _eighty-nine_ , and not eighty-nine _one-hundredths_?
At the same time I am aware that in the way I achieve
this I am restricting the precision only to two signs after the comma
but for the purpose of the program, which is a homework assignment it
is enough.

You lost me.
I think that I don't quite get it! What do you mean by full cents?

That's exactly what I mean. $0.89 is how many cents?
Maybe something like the following although for it is one and the same
thing just written in a different form:

cents( (fl_dollars - dollars) * 100 ) or maybe something like this:

cents( static_cast<int>( fl_dollars * 100 ) % 100 )

Ah! Now you're getting it. Now, put this back into the program.

V
 
A

Alex Buell

Create a class to operate on the US currency. Call the new class Money.
All the data components in this class for dollars and cents are integer
variables (this is where I get confused and don't know how to actually
do the job). Include member functions and constructors to initialize
Money to 0 and to initialize it to fixed values. A member function
should display it in appropriate format. Include member functions that
permit data conversion in both directions:
- from real (float) value (4.5f US) to dollars/cents ( 4 dollar, 50
cents)
- from dollars/cents (4 dollar, 50 cents) to real (float) value (4.5f
US)

Never *ever* use floats or doubles to represent currencies. Why? Because
rounding errors can be a bitch.

Always use integers. It's quite simple to represent US$4.50 as
450 cents, and you don't suffer from rounding errors.
 
A

Alex Buell

May want to add .005 for rounding.

Why?

Use integers to represent value in cents. For those in the UK, use
pennies instead.

Thank **** we ditched the old pounds, shillings and pence system when I
was born. Gawd only knows what UK computers would have made of figures
such as 12 guineas, 4 pounds 6 shillings threepence...
 
F

Fei Liu

Alex said:
Never *ever* use floats or doubles to represent currencies. Why? Because
rounding errors can be a bitch.

Always use integers. It's quite simple to represent US$4.50 as
450 cents, and you don't suffer from rounding errors.
However, floating point arithmatic is unavoidable in currency
calculation. For example, a savings account will have a floating point
number interest rate such as 1.35% or 0.0135. You will have to deal
with FP numbers inevitably.
 
V

Victor Bazarov

Fei said:
Alex said:
[..]
Never *ever* use floats or doubles to represent currencies. Why?
Because rounding errors can be a bitch.

Always use integers. It's quite simple to represent US$4.50 as
450 cents, and you don't suffer from rounding errors.
However, floating point arithmatic is unavoidable in currency
calculation. For example, a savings account will have a floating point
number interest rate such as 1.35% or 0.0135. You will have to deal
with FP numbers inevitably.

Nobody said "avoid FP calculations". But what's needed is to _represent_
the *amounts* in fixed point or integral values and deal with rounding and
balances properly. Once you apply 1.35% interest rate, the montly deposits
should be still calculated and stored and presented to other subsystems as
fixed-point or integral values.

V
 
J

JustBoo

Never *ever* use floats or doubles to represent currencies. Why? Because
rounding errors can be a bitch.

Always use integers. It's quite simple to represent US$4.50 as
450 cents, and you don't suffer from rounding errors.

Does anyone use Binary Coded Decimal (BCD) anymore? I understand there
was no loss of precision because of the way the data is stored and
interpreted, therefore it was good for currency.

I used it, wow, back in the 1980's for a financial app. I've always
read it is slow, but I can't remember having any problems with it back
then, and we're talking 8-10 mhz. 8088 and 8086 processors back then.

I've googled some and there isn't much about it out there. It appears
to have fallen out of favor. :) I quite literally can not find a
clean link to post here. It's all obscure references and buried one
line quotes.

"I put instant coffee in a microwave oven and almost went back in
time." - Steven Wright
 
J

JustBoo

I've googled some and there isn't much about it out there. It appears
to have fallen out of favor. :) I quite literally can not find a
clean link to post here. It's all obscure references and buried one
line quotes.

Egad. Now I get tons of sources... the internet, gotta' love it. :-\ .

http://www.danbbs.dk/~erikoest/bcd.htm

http://en.wikipedia.org/wiki/Binary-coded_decimal

http://hyperphysics.phy-astr.gsu.edu/hbase/electronic/number3.html
Btw, the HyperPhysics and HyperMath site are great. (IMO.)

http://www.tpub.com/neets/book13/53s.htm

Enjoy. :)
 
D

dan2online

k3n3dy said:
Money to 0 and to initialize it to fixed values. A member function
should display it in appropriate format. Include member functions that
permit data conversion in both directions:
- from real (float) value (4.5f US) to dollars/cents ( 4 dollar, 50
cents)
- from dollars/cents (4 dollar, 50 cents) to real (float) value (4.5f
US)

Here it is what I have managed to do up to now although I am not sure

You need to consider the potential precision loss and rounding using
float arithmetic while converting between float and interger in your
case. Some posts here have pointed out these issues.
(1) You can use "double ", at least you avoid precison/rounding in
most cases.
(2) You can also directly decode / encode the binary format of float
point number, then get two precise parts: integer and fraction. I think
it is the best way to avoid these issue whatever software FP library or
hardware FP capability are.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,812
Latest member
GracielaWa

Latest Threads

Top