Thread: suppressing leading zero

  1. #1
    Registered User penney's Avatar
    Join Date
    Jan 2003
    Posts
    47

    suppressing leading zero

    Code:
    /*
    I am attempting to print out a float without a leading zero if it's a 
    */
    float number = .2
    
    /*
    I thought just by specifying the following that this would take care of this:
    */
      float val=.2;
      printf("val = [%7.1f]\n",val);
    
    /*
    However the output that I receive is:
    */
    val = [    0.2]
    
    /*
    As a work around I wrote the following code:
    */
      char buffer[10];
      float val=.2;
      char *dp;
    
      sprintf(buffer,"% 7.1f",val);
      dp = strchr(buffer,'.');
      if( dp && !strncmp(dp-2," 0",2) )
         *(dp-1)=' ';
      printf("val = [%s]\n",buffer);
    
    /*
    And I received the desired output of:
    */
    val = [     .2]
    
    /*
    Is there an simpler way to do this imploring a format specifier that I am somehow overlooking?
    */
    Last edited by penney; 04-08-2004 at 08:49 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. compare function ignoring leading white space
    By mc72 in forum C Programming
    Replies: 5
    Last Post: 11-23-2008, 01:33 PM
  2. suppressing the compilation of the block of code
    By Overworked_PhD in forum C Programming
    Replies: 2
    Last Post: 07-13-2008, 10:24 AM
  3. leading zero's..... ( reading int )
    By sh4k3 in forum C Programming
    Replies: 4
    Last Post: 06-12-2007, 09:03 AM
  4. Removing Leading & Trailing Whitespace
    By EvilGuru in forum C Programming
    Replies: 11
    Last Post: 12-01-2005, 02:59 PM
  5. leading zero on single digit int
    By confuted in forum C++ Programming
    Replies: 2
    Last Post: 07-31-2003, 04:48 PM