By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,037 Members | 2,065 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,037 IT Pros & Developers. It's quick & easy.

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

P: n/a
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();

}

Apr 18 '06 #1
Share this Question
Share on Google+
15 Replies


P: n/a
k3n3dy wrote:
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
--
Please remove capital As from my address when replying by mail
Apr 18 '06 #2

P: n/a
yes my bad it should have been dollars instead of levs, i have
forgotten to change it with dollars.

Apr 18 '06 #3

P: n/a
k3n3dy wrote:
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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Apr 18 '06 #4

P: n/a
Victor Bazarov wrote:
k3n3dy wrote:
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.


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

Brian

--
Please quote enough of the previous message for context. To do so from
Google, click "show options" and use the Reply shown in the expanded
header.
Apr 18 '06 #5

P: n/a
>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

Apr 19 '06 #6

P: n/a
k3n3dy wrote:
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.


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.
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?


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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Apr 19 '06 #7

P: n/a

Victor Bazarov wrote:
k3n3dy wrote:

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.


May want to add .005 for rounding.

Apr 19 '06 #8

P: n/a
Noah Roberts wrote:
Victor Bazarov wrote:
k3n3dy wrote:

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.


May want to add .005 for rounding.


Now look what you've done: you've confused him!
Apr 19 '06 #9

P: n/a
On 19 Apr 2006 13:21:22 -0700 "Noah Roberts" <ro**********@gmail.com>
waved a wand and this message magically appeared:
Victor Bazarov wrote:
k3n3dy wrote:

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.


May want to add .005 for rounding.


Why?

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

Thank fuck 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...

--
http://www.munted.org.uk

Take a nap, it saves lives.
Apr 19 '06 #10

P: n/a
On 17 Apr 2006 16:48:18 -0700 "k3n3dy" <k3****@gmail.com> waved a wand
and this message magically appeared:
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.

--
http://www.munted.org.uk

Take a nap, it saves lives.
Apr 19 '06 #11

P: n/a

Alex Buell wrote:
On 17 Apr 2006 16:48:18 -0700 "k3n3dy" <k3****@gmail.com> waved a wand
and this message magically appeared:
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.

--
http://www.munted.org.uk

Take a nap, it saves lives.

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.

Apr 20 '06 #12

P: n/a
Fei Liu wrote:
Alex Buell wrote:
[..]
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.

--
http://www.munted.org.uk

Take a nap, it saves lives.

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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Apr 20 '06 #13

P: n/a
On Wed, 19 Apr 2006 22:29:37 +0100, Alex Buell
<al********@munted.org.uk> wrote:
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
Apr 20 '06 #14

P: n/a
On Thu, 20 Apr 2006 10:16:32 -0700, JustBoo <Ju*****@BooWho.com>
wrote:
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...c/number3.html
Btw, the HyperPhysics and HyperMath site are great. (IMO.)

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

Enjoy. :-)
Apr 20 '06 #15

P: n/a
k3n3dy wrote:
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.

Apr 20 '06 #16

This discussion thread is closed

Replies have been disabled for this discussion.