Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Divide two integers and get a float
Message
General information
Forum:
Borland C++ Builder
Category:
Standard C++ Library
Miscellaneous
Thread ID:
01003810
Message ID:
01003831
Views:
21
This message has been marked as a message which has helped to the initial question of the thread.
>Consider the following code:
>
>char szRes[25];
>int iOne = 7;
>int iTwo = 6;
>float fRes = iOne / iTwo;
>sprintf(szRes, "%.3f", fRes);
>Application->MessageBoxA(szRes, "Result",0);
>
>
>When I run this code I get a messagebox with the number 1.000. I would like to see the result being 1.167. I underdstand why I get the result I am getting (integer division), but how can I get the result I want (read: the "correct" result)?
>
>Einar


Hi Einar,

I never use Borland C++. But what I know is (maybe) it's because of type casting. So try the following:

float fRes = (FLOAT) iOne / iTwo;

Hope it works
Herman
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform