Page 1 of 1

PostPosted: Fri Oct 18, 2002 12:25 pm
by kac
I have a variable that I am dividing by the value 50. If I enter an initial value of 25 into the variable and divide by 50 then assing the result to an (int) variable it results in a value of 0.

I need to be able to round this to 1 (or round up). The literal value is 5.0 e-1.

Can anyone help with a work around or the proper method for accomplishing this?

PostPosted: Fri Oct 18, 2002 2:02 pm
by jims

It's probably a casting issue, by default if you divide an int with an int you get an int back. So you have to cast them to a float so you get a float back:

<pre id="code"><font face="courier" size="2" id="code">
int roundFloat2Int(float f) {
return (int)(f + 0.5);

handler mainForm.onopen() {
alert( roundFloat2Int((float)25 / (float)50) );
</font id="code"></pre id="code">

- jim