Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 5 of 5

Thread: converting float to integer and nearest 10? like so

  1. #1
    Intern Contributor
    Join Date
    May 2000
    Location
    Canada
    Posts
    71

    converting float to integer and nearest 10? like so

    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?

  2. #2

    Re: converting float to integer and nearest 10? like so

    not really an opengl question, but simple enough:

    Code :
    int floatToDiv10( float in ) {
       return (((int)in)/10) * 10;
    }
     
    void main() {
       printf( "result:%d\n", floatToDiv10( 35.297f ) );
    }

  3. #3
    Guest

    Re: converting float to integer and nearest 10? like so

    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_

  4. #4
    Junior Member Regular Contributor
    Join Date
    Feb 2000
    Posts
    124

    Re: converting float to integer and nearest 10? like so

    Then you just have to change it to

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

  5. #5
    Intern Contributor
    Join Date
    Jul 2000
    Posts
    90

    Re: converting float to integer and nearest 10? like so

    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).]

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •