Arrondir en Language C

Résolu/Fermé
troly Messages postés 180 Date d'inscription samedi 18 décembre 2010 Statut Membre Dernière intervention 10 août 2014 - 27 févr. 2011 à 14:26
troly Messages postés 180 Date d'inscription samedi 18 décembre 2010 Statut Membre Dernière intervention 10 août 2014 - 9 mars 2011 à 17:35
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.



3 réponses

jisisv Messages postés 3645 Date d'inscription dimanche 18 mars 2001 Statut Modérateur Dernière intervention 15 janvier 2017 934
27 févr. 2011 à 20:02

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
0
Bilow Messages postés 1022 Date d'inscription samedi 21 août 2010 Statut Membre Dernière intervention 2 août 2015 117
28 févr. 2011 à 18:09
Tu es bien gentil mais avant :
- Laisse-le un peu chercher :)
- Configuration: Windows Vista Home premium, IE 8 => Il aura du mal à comprendre...
0
jisisv Messages postés 3645 Date d'inscription dimanche 18 mars 2001 Statut Modérateur Dernière intervention 15 janvier 2017 934
28 févr. 2011 à 21:42
Le code C est standard.
0
troly Messages postés 180 Date d'inscription samedi 18 décembre 2010 Statut Membre Dernière intervention 10 août 2014 60
9 mars 2011 à 17:35
Merci beaucoup !!!!
0
Bilow Messages postés 1022 Date d'inscription samedi 21 août 2010 Statut Membre Dernière intervention 2 août 2015 117
27 févr. 2011 à 14:58
-1
troly Messages postés 180 Date d'inscription samedi 18 décembre 2010 Statut Membre Dernière intervention 10 août 2014 60
27 févr. 2011 à 19:06
Comment ça "RTFM"
0
Hxyp Messages postés 401 Date d'inscription vendredi 28 janvier 2011 Statut Membre Dernière intervention 27 avril 2014 54
27 févr. 2011 à 19:41
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.
0
Bilow Messages postés 1022 Date d'inscription samedi 21 août 2010 Statut Membre Dernière intervention 2 août 2015 117
Modifié par Bilow le 28/02/2011 à 18:15
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.
0
Hxyp Messages postés 401 Date d'inscription vendredi 28 janvier 2011 Statut Membre Dernière intervention 27 avril 2014 54
Modifié par Hxyp le 1/03/2011 à 02:55
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.
0