473,396 Members | 1,666 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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

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
15 2957
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
yes my bad it should have been dollars instead of levs, i have
forgotten to change it with dollars.

Apr 18 '06 #3
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
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
>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
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

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

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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Bengt Richter | last post by:
Peculiar boundary cases: >>> 2.0**31-1.0 2147483647.0 >>> int(2147483647.0) 2147483647L >>> int(2147483647L ) 2147483647 >>> >>> -2.0**31
5
by: Yodai | last post by:
Hi all! I have an int that comes with a value like 0x07fa and I have to turn it into a float value of 204.2 decimal to display it.... if I try to divide it by 10 I get bogus numbers. I presume...
7
by: Mike | last post by:
I am trying to calculate a 32bit float value from 4 int values. I sucessfully calcluated a 32bit long value: LONG l32BitData = pData; l32BitData <<= 8; l32BitData |= (byte)pData;...
2
by: Stu Smith | last post by:
A feature we'd like to see in VS 2004 is a compiler warning for equality comparisons on floating values. Why? Because the behaviour allowed under the ECMA spec is somewhat suprising. (Well it...
19
by: Jon Shemitz | last post by:
Is there a difference between a constant like "12.34f" and "(float) 12.34"? In principle, at least, the latter is a double constant being cast to a float; while the two both generate actual...
4
by: chandanlinster | last post by:
I came to know that using format specifiers(in printf or scanf) to convert between float and int is a really bad idea. This is because the printf prints junk values when it encounters such a type...
13
by: Shirsoft | last post by:
I have a 32 bit intel and 64 bit AMD machine. There is a rounding error in the 8th digit. Unfortunately because of the algorithm we use, the errors percolate into higher digits. C++ code is...
8
by: d major | last post by:
I was very puzzled about the conversion between float and long, I cann't understand why a long val can convert to a float, as the below codes show: typedef unsigned long u_long; float val =...
22
by: Bill Reid | last post by:
I just noticed that my "improved" version of sscanf() doesn't assign floating point numbers properly if the variable assigned to is declared as a "float" rather than a "double". (This never...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.