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

Discussion in 'C++' started by k3n3dy, Apr 18, 2006.

  1. k3n3dy

    k3n3dy Guest

    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();

    }
    k3n3dy, Apr 18, 2006
    #1
    1. Advertising

  2. 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
    Victor Bazarov, Apr 18, 2006
    #2
    1. Advertising

  3. k3n3dy

    k3n3dy Guest

    yes my bad it should have been dollars instead of levs, i have
    forgotten to change it with dollars.
    k3n3dy, Apr 18, 2006
    #3
  4. 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
    Victor Bazarov, Apr 18, 2006
    #4
  5. k3n3dy

    Default User Guest

    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.
    Default User, Apr 18, 2006
    #5
  6. k3n3dy

    k3n3dy Guest

    >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
    k3n3dy, Apr 19, 2006
    #6
  7. 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
    Victor Bazarov, Apr 19, 2006
    #7
  8. k3n3dy

    Noah Roberts Guest

    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.
    Noah Roberts, Apr 19, 2006
    #8
  9. 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!
    Victor Bazarov, Apr 19, 2006
    #9
  10. k3n3dy

    Alex Buell Guest

    Re: Float to int conversion by using two int variables forrepresentation of the float variable

    On 17 Apr 2006 16:48:18 -0700 "k3n3dy" <> 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.
    Alex Buell, Apr 19, 2006
    #10
  11. k3n3dy

    Alex Buell Guest

    Re: Float to int conversion by using two int variables forrepresentation of the float variable

    On 19 Apr 2006 13:21:22 -0700 "Noah Roberts" <>
    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 **** 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.
    Alex Buell, Apr 19, 2006
    #11
  12. k3n3dy

    Fei Liu Guest

    Alex Buell wrote:
    > On 17 Apr 2006 16:48:18 -0700 "k3n3dy" <> 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.
    Fei Liu, Apr 20, 2006
    #12
  13. 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
    Victor Bazarov, Apr 20, 2006
    #13
  14. k3n3dy

    JustBoo Guest

    On Wed, 19 Apr 2006 22:29:37 +0100, 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.


    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
    JustBoo, Apr 20, 2006
    #14
  15. k3n3dy

    JustBoo Guest

    On Thu, 20 Apr 2006 10:16:32 -0700, JustBoo <>
    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/hbase/electronic/number3.html
    Btw, the HyperPhysics and HyperMath site are great. (IMO.)

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

    Enjoy. :)
    JustBoo, Apr 20, 2006
    #15
  16. k3n3dy

    dan2online Guest

    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.
    dan2online, Apr 20, 2006
    #16
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Rajesh.Rapaka
    Replies:
    13
    Views:
    1,752
    Patricia Shanahan
    Jul 10, 2006
  2. bd
    Replies:
    0
    Views:
    595
  3. Nick Coghlan
    Replies:
    0
    Views:
    463
    Nick Coghlan
    Dec 6, 2004
  4. Shi Jin

    levels of representation of float and int

    Shi Jin, Dec 8, 2004, in forum: C Programming
    Replies:
    2
    Views:
    304
    Charlie Gordon
    Dec 8, 2004
  5. Carsten Fuchs
    Replies:
    45
    Views:
    1,485
    James Kanze
    Oct 8, 2009
Loading...

Share This Page