Thread: need help rounding a number to hundredths place and storing the new value

  1. #1
    Registered User
    Join Date
    May 2015
    Posts
    5

    need help rounding a number to hundredths place and storing the new value

    Hey guys! I need to round a number to the hundredths place and store the new value. If it is possible I would like change the original value of the variable to the new, rounded value. (i.e., a = 1.674 --> a = 1.67).

    Code:
    #include <stdio.h>
    int main()
    {
        float a, b, c; // variables
            a = 1.674; // a value
            b = 1.322; // b value
            c = a + b; // c value
        printf("%.2f", a); // outputting and reducing 'a' to 2 decimal places
        printf("\n%.2f", b); // outputting and reducing 'b' to 2 decimal places
        printf("\n------"); // extra crap to make things look pretty
        printf("\n%.2f",c); // outputting and reducing 'c' to 2 decimal places
    
        return 0;
    }
    The final output looks like this, but I need the answer to say 2.99.

    1.67
    1.32
    -----
    3.00

    Your help and thoughts are much appreciated!

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    88
    The answer is in this example ,
    it's up to the reader to figure out which one.
    Code:
    prompt > cat d.c ; gcc -Wall -g d.c -o d ; ./d
    #include <stdio.h>
    
    #define PRINT_INT_EXP(xx_exp) \
      fprintf(stdout,"%-40s = %10ld\n", # xx_exp,(long)(xx_exp))
    #define PRINT_DBL_EXP(xx_exp) \
      fprintf(stdout,"%-40s = %17.6f\n", # xx_exp,(double)(xx_exp))
    int
    main(int argc, char **argv, char **envr)
    {
       int   a   = 3;
       int   b   = 4;
       int   c   = 5;
       float a_2 = 1.674; // a_2 value
       float b_2 = 1.322; // b_2 value
       float c_2 = a_2 + b_2; // c_2 value
       long  a_3 = a_2 * 10000;
       long  b_3 = b_2 * 10000;
       long  c_3 = a_3 + b_3;
       float third = 1.0/3.0;
       float sixth = 1.0/6.0;
    
    
       PRINT_INT_EXP(a);
       PRINT_INT_EXP(b);
       PRINT_INT_EXP(c);
       PRINT_INT_EXP(a<b+c);
       PRINT_INT_EXP(a<(b+c))
       PRINT_INT_EXP(a*a + b*b);
       PRINT_INT_EXP(c*c);
       PRINT_INT_EXP(a_3);
       PRINT_INT_EXP(b_3);
       PRINT_INT_EXP(c_3);
       PRINT_DBL_EXP(a_2);
       PRINT_DBL_EXP(b_2);
       PRINT_DBL_EXP(c_2);
       PRINT_INT_EXP((c_2 * 100));
       PRINT_DBL_EXP((c_2 * 100)/100);
       PRINT_DBL_EXP(((long)(sixth *100))/100.0);
       PRINT_DBL_EXP(((long)(third *100))/100.0);
       PRINT_DBL_EXP(((long)(2*third *100))/100.0);
       PRINT_DBL_EXP(((long)(2*third *100.0 + .5))/100.0);
       PRINT_DBL_EXP(((long)(2*sixth *100))/100.0);
       PRINT_DBL_EXP(third);
       PRINT_DBL_EXP(sixth);
       PRINT_INT_EXP((sixth * 1000)  );
       PRINT_INT_EXP((sixth * 1000) + .5);
       PRINT_INT_EXP(third * 1000);
       PRINT_INT_EXP((third * 1000)+.5);
       PRINT_INT_EXP(argc);
       return(0);
    
    }
    a                                        =          3
    b                                        =          4
    c                                        =          5
    a<b+c                                    =          1
    a<(b+c)                                  =          1
    a*a + b*b                                =         25
    c*c                                      =         25
    a_3                                      =      16740
    b_3                                      =      13220
    c_3                                      =      29960
    a_2                                      =          1.674000
    b_2                                      =          1.322000
    c_2                                      =          2.996000
    (c_2 * 100)                              =        299
    (c_2 * 100)/100                          =          2.996000
    ((long)(sixth *100))/100.0               =          0.160000
    ((long)(third *100))/100.0               =          0.330000
    ((long)(2*third *100))/100.0             =          0.660000
    ((long)(2*third *100.0 + .5))/100.0      =          0.670000
    ((long)(2*sixth *100))/100.0             =          0.330000
    third                                    =          0.333333
    sixth                                    =          0.166667
    (sixth * 1000)                           =        166
    (sixth * 1000) + .5                      =        167
    third * 1000                             =        333
    (third * 1000)+.5                        =        333
    argc                                     =          1
    prompt >

  3. #3
    Registered User
    Join Date
    May 2015
    Posts
    5
    I got it! Thank you!
    Code:
    #include <stdio.h>
    int main()
    {
        float a, b, c; // variables
            a = 1.674; // a value
            a = (int) (a * 100); // converts 'a' to the integer 167
            a = (float) (a * .01); // converts 'a' to the number 1.67
            b = 1.322; // b value
            b = (int) (b * 100); // converts 'b' to the integer 132
            b = (float) (b * .01); // converts 'b' to the number 1.32
            c = a + b;
        printf("%.2f", a); // outputting and reducing 'a' to 2 decimal places
        printf("\n%.2f", b); // outputting and reducing 'b' to 2 decimal places
        printf("\n------"); // extra crap to make things look pretty
    
        printf("\n%.2f",c); // outputting and reducing 'c' to 2 decimal places
    
        return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Storing a 10 digit number in C
    By thebenman in forum C Programming
    Replies: 1
    Last Post: 12-09-2014, 11:11 AM
  2. Storing a very large number
    By DarkEmpire in forum C Programming
    Replies: 5
    Last Post: 02-26-2012, 09:50 AM
  3. Need help rounding to the hundreth decimal place
    By bardler in forum C++ Programming
    Replies: 15
    Last Post: 02-27-2008, 08:13 AM
  4. Help with rounding a number
    By nickk in forum C Programming
    Replies: 3
    Last Post: 06-02-2004, 11:44 AM
  5. Storing the whole number
    By flip114 in forum C Programming
    Replies: 3
    Last Post: 10-21-2003, 08:50 AM