Float to nearest int | Sololearn: Learn to code for FREE!
Nouvelle formation ! Tous les codeurs devraient apprendre l'IA générative !
Essayez une leçon gratuite
0

Float to nearest int

How can i change a float result to nearest integer? Like 45.49 -> 45 or 45.51 -> 46.

25th Aug 2023, 11:51 AM
Francesco Rossi
Francesco Rossi - avatar
5 Réponses
+ 4
Francesco Rossi Use the `round()` function from the math library to round a float to the nearest integer. Otherhand, If you're having trouble in code, share your code and describe what error message you're receiving.
25th Aug 2023, 12:31 PM
Darpan kesharwaniūüáģūüá≥[Inactiveūüďö]
Darpan kesharwaniūüáģūüá≥[Inactiveūüďö] - avatar
+ 1
Francesco Rossi You need to complete this course. https://www.sololearn.com/learn/courses/c-intermediate https://www.sololearn.com/learn/courses/c-introduction Note that..`stdio.h` and `round()` are not the same,they serve different purposes in C programming. The `stdio.h` is a library in C, which provides functions for input and output operations, such as reading and writing to the console. It includes functions like `printf()` and `scanf()`. Other hand, the `round()` is a function ,round function is a part of the `math.h` library in C. It is used for rounding floating-point numbers to the nearest integer & This function takes a floating-point number as input and returns the rounded integer value.
25th Aug 2023, 2:42 PM
Darpan kesharwaniūüáģūüá≥[Inactiveūüďö]
Darpan kesharwaniūüáģūüá≥[Inactiveūüďö] - avatar
0
How if i have to use a stdio.h library only? It is for an exercise here
25th Aug 2023, 2:07 PM
Francesco Rossi
Francesco Rossi - avatar
0
#include <stdio.h> #include <math.h> int main() { float number = 45.49; int roundedNumber = round(number); printf("original number: %.2f\n", number); printf("rounded number: %d\n", roundedNumber); return 0; }
25th Aug 2023, 4:18 PM
Per Bratthammar
Per Bratthammar - avatar
0
Thank you so much guys, really kind, someone could tell me how code "halloween candy" exercise in code coach? It is an Easy task but i can't do it
25th Aug 2023, 7:52 PM
Francesco Rossi
Francesco Rossi - avatar