Solved float to int

Question that is answered or resolved.

msmalik681

OpenBOR Developer
Staff member
is there any way to convert a float value to integer or at least trim off all the data after the decimal place?


besides a obvious long if statement converting manually or bitwise functions!
 
I thought it was fine but the calculation is a bit off below is a calculation as a float its 20.00000 but when I apply your calculation it converts to 19 not 20.



int fl = (1-0.80)*100;
int def = fl%(fl+1);
settextobj(0, 35, 30, 3, 9999999999, def); //19?
 
Ok, you can use this func:
Code:
int truncA(int num) {
    int sign = 1;
    float min = 0.00000001;

    if ( num < 0 ) {
        sign = -1;
        num *= -1;
        min *= -1;
    }

    num += min;
    num = num%(num+1);
    num *= sign;

    return num;
}

Or if you want, use the new r4424.
added trunc() and round()
example

trunc( round(x) )
 
Back
Top Bottom