Thread: display double w/o trailing zeros?

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    10

    Unhappy display double w/o trailing zeros?

    I'm trying to display a list of doubles with out the trailing zeros.
    This snipet is just the declaration and printf...the actual code is fine
    I just need to modify these intergers for the printf
    Code:
       double sq = 0, bb = 0, ac4 = 0, z = 0;
       long sq2 = 0, x1 = 0, x2 = 0;
       printf("%d+%di, %d-%di.\n\n", x1, sq, x1, sq);
    This is part of a quadradic solver. I'm inputting x^2+2x+2
    and keep getting:

    1.000000+1.0000000i, 1.0000000-1.0000000i

    instead of;

    1+1i, 1-1i

    could anyone assist with a method to truncate the trailing zeros?
    Last edited by Navar; 02-09-2006 at 01:56 AM. Reason: clarify

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    But your code cannot produce that output, %d does not print floats.

  3. #3
    Registered User
    Join Date
    Jan 2006
    Posts
    10

    Unhappy My appoligies

    Code:
       double sq = 0, bb = 0, ac4 = 0, z = 0;
       double sq2 = 0, x1 = 0, x2 = 0;
       printf("%f+%fi, %f-%fi.\n\n", x1, sq, x1, sq);
    However, the %d was also producing the same result.
    Probly my complier.

    still getting output w/ trailing zeros

    any idea how to truncate the trailing zeros from a float
    in printf?
    Last edited by Navar; 02-09-2006 at 12:47 PM.

  4. #4
    Registered User
    Join Date
    Oct 2005
    Posts
    38
    http://cplusplus.com/ref/cstdio/printf.html

    precision:
    for e, E, f types: number of digits to be printed after de decimal point. (if nothing specified default is 6).

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    If you want no decimal digits to be displayed, print as an int:
    Code:
    double x = 5.2;
    printf("%d\n", (int)x);
    Output:
    Code:
    5
    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.

  6. #6
    Registered User
    Join Date
    Feb 2006
    Posts
    43
    I second dwks' response. Or you can use %.nf instead of %f, where the n is the number of decimal places you want displayed. For example, if you want two decimal places it would be %.2f
    Note the decimal point(.) placed between the % sign and the number.
    -Crazed

  7. #7
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    I like %g.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  8. #8
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    ".nf" can still print with training zeros. An alternative is to print to a string buffer (ensure it at least long enough to hold the value when printed) using sprintf(), strip any training zeros, and then print the resultant string.

  9. #9
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    Code:
    #include<stdio.h>
    
    int main()
    {
            float u=10.0000;
    
            printf("%.0f",u);
            
            getchar();
            return 0;
    }
    /*myoutput
    10
    */
    ssharish2005

  10. #10
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    For comparison.
    Code:
    #include <stdio.h>
    
    int main ( void )
    {
       int i;
       for ( i = 1; i < 5; ++i )
       {
          double d = 1.0 / i;
          printf ( "d = %g (%%g) = %f (%%f) = %.0f (%%.0f) = %.2f (%%.2f)\n", 
                   d, d, d, d );
       }
       return 0;
    }
    
    /* my output
    d = 1 (%g) = 1.000000 (%f) = 1 (%.0f) = 1.00 (%.2f)
    d = 0.5 (%g) = 0.500000 (%f) = 0 (%.0f) = 0.50 (%.2f)
    d = 0.333333 (%g) = 0.333333 (%f) = 0 (%.0f) = 0.33 (%.2f)
    d = 0.25 (%g) = 0.250000 (%f) = 0 (%.0f) = 0.25 (%.2f)
    */
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  11. #11
    Registered User
    Join Date
    Jan 2006
    Posts
    10

    Talking

    I did get it to print specific number or decimals using %.nf...

    %g does exactly what I want -
    Code:
    double sq = 0, bb = 0, ac4 = 0, z = 0;
    
    sq = sqrt(bb - ac4);
    z = -0.5*(b + sq);
    x1 = z / a;
    x2 = c / z;
    
    printf("     Rational roots:     %g\n", x1);
    printf("                         %g\n\n", x1, x2);
    this returns no trailing zeros but give the most accuracy for
    decimals up to 4 bytes. Thanks for your help everyone

    *I'd post the entire code but this is a common homework
    problem for intro C programing.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 04-25-2008, 02:45 PM
  2. Need some help/advise for Public/Private classes
    By nirali35 in forum C++ Programming
    Replies: 8
    Last Post: 09-23-2006, 12:34 PM
  3. How to display 23 bytes long double answer in C?
    By CLIE_ZETA in forum C Programming
    Replies: 3
    Last Post: 11-18-2001, 12:11 PM
  4. error message, what does it mean?
    By Shy_girl_311 in forum C++ Programming
    Replies: 5
    Last Post: 11-09-2001, 09:54 PM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM