CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: conversions

  1. #1
    Join Date
    Oct 2005
    Posts
    173

    Question conversions

    Hi again

    For love of god, how can I convert a integer to a float??
    I have this function that returns always zero:
    int iDrawIHeight = 480;
    iScreenHeight = 768;
    float iPorcentage = ((float)(iDrawedHeight/iScreenHeight));
    The result that must returned is 0.62, but it returns zero. Why?

  2. #2
    Join Date
    Aug 2005
    Posts
    104

    Re: conversions

    Because 480/768 is zero, when you're dealing with integers. Convert them to float before doing the division. Like this:

    float iPorcentage = ((float)iDrawedHeight)/(float)iScreenHeight;

  3. #3
    Join Date
    Sep 2004
    Posts
    561

    Re: conversions

    What is happening is that you are taking two integer values 480/768 which equals 0 and then casting that 0 value to a float. You need to do what torfil mentioned.

    The second cast "(float)iScreenHeight" is optional since iScreenHeight will automatically be converted to a float.

  4. #4
    Join Date
    Oct 2005
    Posts
    173

    Thumbs up Re: conversions

    thanks guys, the code now works

  5. #5
    Join Date
    Feb 2005
    Location
    "The Capital"
    Posts
    5,306

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