How to remove all decimal places?

This is a discussion on How to remove all decimal places? within the C Programming forums, part of the General Programming Boards category; Can someone tell me why this code doesn't work... Code: #include <stdio.h> int main(void) { int a, i = 5.156546; ...

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    3

    How to remove all decimal places?

    Can someone tell me why this code doesn't work...

    Code:
    #include <stdio.h>
    
    int main(void) {
    
    	int a, i = 5.156546;
    
    	a = trunc(i);
    
    	printf("%d", a);
    
    	return(0);
    }

    And is there any other way to remove all numbers after a decimal place without using the trunc() function??

  2. #2
    cwr
    cwr is offline
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    Well for a start trunc takes a double and returns a double, and you're playing with ints.

    Secondly, i is an int (integer), so it can't store a value of 5.156546 (not an integer).

  3. #3
    Registered User
    Join Date
    May 2006
    Posts
    12
    int a, i = 5.156546;
    i = 5 because i is integer.
    try
    int a;
    double d = 5.156546;
    a = (int) d;
    or print
    printf("%.0f\n", d);

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    How to remove all decimal places?
    Cast to an int:
    Code:
    /* prints "3" */
    printf("%f", (int)3.14159265358979636);
    If you want to round the number, not truncuate it, use
    Code:
    /* prints "3" */
    printf("%d", (int)(3.14159265358979636 + .5));
    /* prints "4" */
    printf("%d", (int)(3.6 + .5));
    If you want to round the number to, say, 2 decimal places, use
    Code:
    /* prints "3.14" */
    printf("%f", (int)(3.14159265358979636 * 100 + .5) / 100.0);
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    cwr
    cwr is offline
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    You don't really need the convoluted arithmetic:
    Quote Originally Posted by dwks
    Cast to an int:
    If you want to round the number, not truncuate it, use
    Code:
    /* prints "3" */
    printf("%d", (int)(3.14159265358979636 + .5));
    /* prints "4" */
    printf("%d", (int)(3.6 + .5));
    Code:
    /* prints "3" */
    printf("%.0f", 3.14159265);
    /* prints "4" */
    printf("%.0f", 3.6);
    Quote Originally Posted by dwks
    If you want to round the number to, say, 2 decimal places, use
    Code:
    /* prints "3.14" */
    printf("%f", (int)(3.14159265358979636 * 100 + .5) / 100.0);
    Code:
    /* prints "3.14" */
    printf("%.2f", 3.14159265358979);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Decimal places
    By Gordon in forum Windows Programming
    Replies: 9
    Last Post: 06-08-2008, 09:04 AM
  2. %g doesn't print as many decimal places as %f
    By octoc in forum C Programming
    Replies: 1
    Last Post: 03-31-2008, 12:16 PM
  3. Decimal places
    By Gordon in forum Windows Programming
    Replies: 4
    Last Post: 09-28-2007, 10:03 AM
  4. Decimal places on Floating point number
    By manutdfan in forum C Programming
    Replies: 1
    Last Post: 10-29-2006, 11:56 AM
  5. Replies: 10
    Last Post: 06-12-2002, 03:15 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21