How to round a float to N decimals in C

3 Answers

0 votes
#include <stdio.h>

int main(void) {
    float number = 82.79521;

    printf("%.2f", number);

    return 0;
}




/*
run:

82.80

*/

 



answered Jul 29, 2022 by avibootz
0 votes
#include <stdio.h>

int main(void) {
    float number = 82.73521;

    printf("%.2f", number);

    return 0;
}




/*
run:

82.74

*/

 



answered Jul 29, 2022 by avibootz
0 votes
#include <stdio.h>

int main(void) {
    float number = 82.73165;

    printf("%.2f", number);

    return 0;
}




/*
run:

82.73

*/

 



answered Jul 29, 2022 by avibootz

Related questions

4 answers 198 views
198 views asked Jul 29, 2022 by avibootz
3 answers 177 views
1 answer 143 views
1 answer 261 views
1 answer 143 views
143 views asked Aug 7, 2019 by avibootz
1 answer 197 views
197 views asked Jan 17, 2019 by avibootz
...