Printing float number with incremented precision

This is a discussion on Printing float number with incremented precision within the C Programming forums, part of the General Programming Boards category; Hi The following code gives the corresponding print: Code: floatValue = 12345.67890; printf("%.1f\n", floatValue); printf("%.2f\n", floatValue); printf("%.3f\n", floatValue); printf("%.4f\n", floatValue); ...

  1. #1
    Registered User
    Join Date
    Sep 2012
    Posts
    2

    Printing float number with incremented precision

    Hi

    The following code gives the corresponding print:
    Code:
    floatValue = 12345.67890;
    
    printf("%.1f\n", floatValue);
    printf("%.2f\n", floatValue);
    printf("%.3f\n", floatValue);
    printf("%.4f\n", floatValue);
    printf("%.5f\n", floatValue);
    Print:
    12345.7
    12345.68
    12345.679
    12345.6789
    12345.67890

    Is there a way to make this precision increment in a loop instead?
    i.e.
    Code:
    for(int i=1; i < 6; i++){
        printf("%.if\n", floatValue);
        i++;
    }
    BR,
    Flanderso

  2. #2
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,656
    It's actually in the manual if you look.

    printf("%.*f\n", i, floatValue);

  3. #3
    Registered User
    Join Date
    Sep 2012
    Posts
    2
    Thanks a lot whiteflags!

    Btw, what manual?

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,484
    Ages ago, you used to place the cursor on say printf and press the F1 key to get information on that function.

    Nowadays, you can typically use a search engine with the word 'man' and the function of interest.
    man printf - Google Search
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,299
    Your example is misleading by the way.
    For it to output that, the variable floatValue must actually be declared as a double, not a float.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  6. #6
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    383
    Quote Originally Posted by iMalc View Post
    Your example is misleading by the way.
    For it to output that, the variable floatValue must actually be declared as a double, not a float.
    What? I thought for sure that a float is promoted to double when passed to a variadic function (or when there is no function declaration) like printf.

  7. #7
    qny
    qny is offline
    Registered User
    Join Date
    Sep 2012
    Posts
    355
    Quote Originally Posted by christop View Post
    What? I thought for sure that a float is promoted to double when passed to a variadic function (or when there is no function declaration) like printf.
    I think what iMalc means is that, usually, float values do not have the kind of precision indicated by the example.
    For an 32-bit IEEE floating point number, with 23+1 binary digits of precision, the precision of the decimal expansion (~7 digits) is not enough for the result shown.

  8. #8
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,299
    Quote Originally Posted by qny View Post
    I think what iMalc means is that, usually, float values do not have the kind of precision indicated by the example.
    For an 32-bit IEEE floating point number, with 23+1 binary digits of precision, the precision of the decimal expansion (~7 digits) is not enough for the result shown.
    Yes exactly.
    It might be promoted to double for the printf call, something that was not of interest to me at the time. If the values were stored in floats then the promoted value would still only have about 6 significant decimal digits in the output due to the time that the value spent being held just inside the float variable. Therefore the variable is definitely a double.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Manipulating individual bits in a double precision number
    By thetinman in forum C++ Programming
    Replies: 4
    Last Post: 05-27-2010, 07:19 AM
  2. Printing float number in MessageBox with Unicode
    By mvanrijnbach in forum Windows Programming
    Replies: 10
    Last Post: 02-18-2010, 12:25 AM
  3. float accumulator precision?
    By chunlee in forum C Programming
    Replies: 8
    Last Post: 11-12-2004, 01:25 AM
  4. float/double variable storage and precision
    By cjschw in forum C++ Programming
    Replies: 4
    Last Post: 07-28-2003, 06:23 PM
  5. setting a float to 4 digit decimal precision
    By river-wind in forum C Programming
    Replies: 8
    Last Post: 01-21-2002, 04:03 PM

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