|
-
November 10th, 2005, 03:04 PM
#1
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?
-
November 10th, 2005, 03:07 PM
#2
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;
-
November 10th, 2005, 03:17 PM
#3
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.
-
November 10th, 2005, 09:58 PM
#4
Re: conversions
thanks guys, the code now works
-
November 11th, 2005, 12:45 AM
#5
Re: conversions
Use static_cast<>. See the details here - msdn - static_cast<> operator. Regards.
Can you help me with my homework assignment?, Before you post!, Use code tags, How to post!, Codeguru technical FAQs, C++ FAQ Lite, Stroustrup: C++ Style and Technique FAQ, Guru of the Week, Comeau C and C++ FAQs, Comeau C++ Templates FAQs, CUJ @ DDJ, Spam threshold
My Blogs : Learning C++ is fun | Abnegator's reflections
Open Threads : C++ Aha! Moments | Nature of work in C++?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|