PDA

View Full Version : converting float to integer and nearest 10? like so

Schlogenburg
07-13-2000, 01:09 PM
say I have float:
float a = 35.297;

I want to convert to integer and to the nearest 10 like so:
results in b:

30

how can I do this to any given float?

phlake
07-13-2000, 01:28 PM
not really an opengl question, but simple enough:

int floatToDiv10( float in ) {
return (((int)in)/10) * 10;
}

void main() {
printf( "result:%d\n", floatToDiv10( 35.297f ) );
}

07-13-2000, 07:53 PM
Originally posted by Schlogenburg:
say I have float:
float a = 35.297;

I want to convert to integer and to the nearest 10 like so:
results in b:

30

how can I do this to any given float?

um shouldn't the answer be 40?? being _NEAREST_

Marc
07-14-2000, 04:25 AM
Then you just have to change it to

int floatToDiv10( float in ){
return (((int)(in+5.0f))/10)*10;
}

foobar
07-15-2000, 07:57 PM
That only works for positve numbers. Just use 'int rint(float f)' from the maths library.

[This message has been edited by foobar (edited 07-15-2000).]