Arrondir en Language C

Résolu
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!)

3 réponses

  1. jisisv Messages postés 3678 Statut Modérateur 936
     
    
    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
    1. Bilow Messages postés 171 Date d'inscription   Statut Membre Dernière intervention   117
       
      Tu es bien gentil mais avant :
      - Laisse-le un peu chercher :)
      - Configuration: Windows Vista Home premium, IE 8 => Il aura du mal à comprendre...
      0
    2. jisisv Messages postés 3678 Statut Modérateur 936
       
      Le code C est standard.
      0
  2. troly Messages postés 197 Statut Membre 60
     
    Merci beaucoup !!!!
    0
  3. Bilow Messages postés 171 Date d'inscription   Statut Membre Dernière intervention   117
     
    -1
    1. troly Messages postés 197 Statut Membre 60
       
      Comment ça "RTFM"
      0
    2. Bilow Messages postés 171 Date d'inscription   Statut Membre Dernière intervention   117
       
      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
    3. Hxyp Messages postés 401 Date d'inscription   Statut Membre Dernière intervention   54
       
      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