How to rounds down a float to an integer in C

2 Answers

0 votes
#include <stdio.h>
#include <math.h> 

int main(int argc, char **argv) 
{
    float value, result;
    
    value = 5.2;
    result = floor(value);
    printf("The floor of %.2f is %.2f\n", value, result); // 5.00
    
    value = 5.4;
    result = floor(value);
    printf("The floor of %.2f is %.2f\n", value, result); // 5.00
    
    value = 5.5;
    result = floor(value);
    printf("The floor of %.2f is %.2f\n", value, result); // 5.00

    value = 5.6;
    result = floor(value);
    printf("The floor of %.2f is %.2f\n", value, result); // 5.00

    return(0);
}


/*
run:

The floor of 5.20 is 5.00
The floor of 5.40 is 5.00
The floor of 5.50 is 5.00
The floor of 5.60 is 5.00

*/


answered May 20, 2015 by avibootz
0 votes
#include <stdio.h> 

int main(int argc, char **argv) 
{
    float value, result;
    
    value = 5.2;
    result = (int)value;
    printf("The floor of %.2f is %.2f\n", value, result); // 5.00
    
    value = 5.4;
    result = (int)value;
    printf("The floor of %.2f is %.2f\n", value, result); // 5.00
    
    value = 5.5;
    result = (int)value;
    printf("The floor of %.2f is %.2f\n", value, result); // 5.00

    value = 5.6;
    result = (int)value;
    printf("The floor of %.2f is %.2f\n", value, result); // 5.00

    return(0);
}


/*
run:

The floor of 5.20 is 5.00
The floor of 5.40 is 5.00
The floor of 5.50 is 5.00
The floor of 5.60 is 5.00

*/


answered May 20, 2015 by avibootz

Related questions

1 answer 179 views
179 views asked May 20, 2015 by avibootz
1 answer 143 views
143 views asked Jan 12, 2017 by avibootz
1 answer 112 views
2 answers 209 views
209 views asked May 1, 2015 by avibootz
1 answer 266 views
...