training

Code I wrote during training
git clone git://git.bitsmanent.org/training
Log | Files | Refs | README

round4.c (895B)


      1 /* Esercizio 5.11 */
      2 
      3 #include <stdio.h>
      4 #include <math.h>
      5 
      6 float roundToInteger(float);
      7 float roundToTenths(float);
      8 float roundToHundreths(float);
      9 float roundToThousandths(float);
     10 
     11 int main()
     12 {
     13    float num = 21.199;
     14 
     15    do {
     16       printf("%.3f %.3f %.3f %.3f\n",
     17 	 roundToInteger(num),
     18 	 roundToTenths(num),
     19 	 roundToHundreths(num),
     20 	 roundToThousandths(num));
     21 
     22       printf("Give me a number (0 to end): ");
     23       scanf("%f", &num);
     24    } while(num * 100 / 100);
     25 
     26    return 0;
     27 } /* E0F main */
     28 
     29 float roundToInteger(float n)
     30 {
     31    return floor( n * 1 + .5) / 1;
     32 } /* eof roundToInteger */
     33 
     34 float roundToTenths(float n)
     35 {
     36    return floor( n * 10 + 5.) / 10;
     37 } /* eof roundToTenths */
     38 
     39 float roundToHundreths(float n)
     40 {
     41    return floor( n * 100 + .5) / 100;
     42 } /* eof roundToHundreths */
     43 
     44 float roundToThousandths(float n)
     45 {
     46    return floor( n * 1000 + .5) / 1000;
     47 } /* eof roundToThousandths */
     48