CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Apr 1999
    Location
    Germany
    Posts
    53

    type conversion problem

    Hi,

    I have a problem with following code:

    void calculateamount( long* l_amount )
    {
    double amount;

    // amount gets calculated here
    // the result is: 19.67

    amount *= 100;
    *l_amount = amount;

    // then l_amount = 1966 and not 1967. WHY ????
    // how can i prevent it?

    }




  2. #2
    Join Date
    Apr 1999
    Posts
    3

    Re: type conversion problem

    I believe that problem is that the rounding that occurs on 196.7 is just a truncation. If this is the case simply add 0.5 to amount:

    amount *= 100;
    amount += 0.5;
    *l_amount = amount;

    This should solve the rounding problem



  3. #3
    Join Date
    Apr 1999
    Location
    Germany
    Posts
    53

    Re: type conversion problem

    when I multiply 19.67 with 100 the result is an integer: 1967 and not 196.7 as u wrote.

    so this won't help me.


  4. #4
    Join Date
    Apr 1999
    Posts
    8

    Re: type conversion problem

    Well, not if the amount is actually 1966.9. Then it is rounded to 1967 if you print the double value, but when converted to an integer it is truncated to 1966.

    The code:

    double amount = 19.67
    amount *= 100;
    long l_amount = amount;

    works just fine, l_amount is 1967. I can't see any other problem than the truncation...


    /Michael Grundberg
    M.Sc. Computer Science
    Visionova IT-System, Sweden
    [email protected]


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured