Arrondir en Language C
Résolu
troly
Messages postés
197
Statut
Membre
-
troly Messages postés 197 Statut Membre -
troly Messages postés 197 Statut Membre -
Bonjour,
Je voudrais arrondir un nombre selon ce qu'il a après la virgule c'est à dire si la variable = 3.6 alors elle devient 4, mais si la variable = 3.4 alors elle devient 3.
--
Je suis le meilleur au monde dans ce que je fait (enfin je crois!)
Je voudrais arrondir un nombre selon ce qu'il a après la virgule c'est à dire si la variable = 3.6 alors elle devient 4, mais si la variable = 3.4 alors elle devient 3.
--
Je suis le meilleur au monde dans ce que je fait (enfin je crois!)
3 réponses
-
johand@osiris:~/src/ccm$ cat round.c #include <stdlib.h> #include <stdio.h> #include <math.h> int main(int argc, char *argv[]) { printf("%f --> %f\n", -3.4 , floor(-3.4 +0.5)); printf("%f --> %f\n", -3.7 , floor(-3.7 +0.5)); printf("%f --> %f\n", 3.4 , floor(3.4 +0.5)); printf("%f --> %f\n", 4.5 , floor(4.5 +0.5)); return 0; } johand@osiris:~/src/ccm$ make round cc round.c -o round johand@osiris:~/src/ccm$ ./round -3.400000 --> -3.000000 -3.700000 --> -4.000000 3.400000 --> 3.000000 4.500000 --> 5.000000 -
-
-
-
Bonjour, ça doit être pour "read the fucking manual" -> http://www.readthefuckingmanual.com/ ...
Sur cette page : http://jkorpela.fi/round.html vous trouverez macro et explications pour arrondir. -
Read The Fucking Manual (de l'Anglais Lis le put*** de manuel !) n'est pas méchant, cela veut juste dire que tu aurais pu chercher un peu avant de poser ta question. Google > Arrondir en C > 2 minutes et t'as trouvé... Même chose pour "RTFM" : 2 minutes sur Google (ou autre moteur de recherche) et t'as trouvé. Prochaine fois, cherche un peu plus ;)
Tu tomberas sur la fonction round(); qui est ce que tu cherches. -
En fait après recherches il existe une fonction standard dans la bibliothèque math.h qui arrondit :
double nearbyint(double x)#include <stdio.h> #include <math.h> int main() { printf("3.6 : %d\n",(int)nearbyint(3.6)); printf("3.4 : %d\n",(int)nearbyint(3.4)); return 0; }
3.6 : 4
3.4 : 3
Edit : je n'avais pas terminé de lire le standard avec "round", Bilow a raison pour le RTFM vrai que c'est chiant mais tout est dedans :
7.12.9.6 The round functions Synopsis 1 #include <math.h> double round(double x); float roundf(float x); long double roundl(long double x); Description 2 The round functions round their argument to the nearest integer value in floating-point format, rounding halfway cases away from zero, regardless of the current rounding direction. Returns 3 The round functions return the rounded integer value.
-