Thread: printf , decimal places

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    1

    printf , decimal places

    Hi ,

    I have a beginners question , I would like to print variable with 2 decimal places.

    printf("%.2f",a); all nice and dandy , but what if i want to set decimal places dynamically.

    Example (i know this doesnt work, just for demonstration what I need ):

    int dec=2;
    printf("%.%decf",dec,a);

    Is it posible to do it this way ?

  2. #2
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Code:
    printf("%.*f\n",dec,a);
    Read the doc.

  3. #3
    Programming King Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Middle of NoWhere
    Posts
    320
    Quote Originally Posted by Bayint Naung View Post
    Code:
    printf("%.*f\n",dec,a);
    Read the doc.
    Can you kindly explain it's working to me please....
    Thanks.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Use *

    Read the manual
    Quote Originally Posted by a printf manual
    The precision

    An optional precision, in the form of a period ('.') followed by an optional decimal digit string. Instead of a decimal digit string one may write '*' or '*m$' (for some decimal integer m) to specify that the precision is given in the next argument, or in the m-th argument, respectively, which must be of type int. If the precision is given as just '.', or the precision is negative, the precision is taken to be zero. This gives the minimum number of digits to appear for d, i, o, u, x, and X conversions, the number of digits to appear after the radix character for a, A, e, E, f, and F conversions, the maximum number of significant digits for g and G conversions, or the maximum number of characters to be printed from a string for s and S conversions.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by tortelini View Post
    Hi ,

    I have a beginners question , I would like to print variable with 2 decimal places.

    printf("%.2f",a); all nice and dandy , but what if i want to set decimal places dynamically.

    Example (i know this doesnt work, just for demonstration what I need ):

    int dec=2;
    printf("%.%decf",dec,a);

    Is it posible to do it this way ?
    You do realize that you can compose the formatting string dynamically...
    Code:
    char s[50] = {0};
    
    strcpy(s,"%d %.2f");
    printf(s,x,y);
    Last edited by CommonTater; 03-07-2011 at 08:12 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 01-29-2011, 04:54 AM
  2. Trouble with two two dimensional arrays
    By scmurphy64 in forum C Programming
    Replies: 5
    Last Post: 12-06-2009, 06:57 PM
  3. making it portable.....?
    By ShadeS_07 in forum C Programming
    Replies: 11
    Last Post: 12-24-2008, 09:38 AM
  4. get keyboard and mouse events
    By ratte in forum Linux Programming
    Replies: 10
    Last Post: 11-17-2007, 05:42 PM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM