Doubt with float

1
#include<stdio.h>

int main ( void ){
float y = 12.0809;
printf("The number %.2f is a float\n");
return 0;
}

I know that %.2f is used to consider only two decimal places, but I'd like to know what an integer serves before the period. For example, %7.2f or %3.2f . If the number that comes after the point is used to limit the number of decimal places, what is the number that comes before the point?

    
asked by anonymous 07.01.2017 / 03:37

3 answers

4

The number that comes after % and before . is the number of minimum characters that the number goes in the resulting string. For example:

#include <stdio.h>

int main(void) {
    printf("Teste 1: '%3.2f' - A\n", 12.0809);
    printf("Teste 2: '%8.2f' - B\n", 12.0809);
    printf("Teste 3: '%08.2f' - C\n", 12.0809);
    return 0;
}

Here's the output:

Teste 1: '12.08' - A
Teste 2: '   12.08' - B
Teste 3: '00012.08' - C

Note that in test 2, there are eight characters between the first% w and% second. Obviously, if the resulting string were to exceed this, it would have as many characters as needed (as in Test 1).

Placing a zero just after the% cos_de%, instead of filling with spaces, the fill occurs with zeros, as in test 3.

See here working on ideone.

Relevant reference: link

    
07.01.2017 / 04:01
2

What comes after . is the precision of the decimal points. What comes before . is the digit width your program will leave to display the value.

For example, the statement: printf("%9.6f", myFloat) specifies that the width of the display digits will be 9 spaces. This means that you will have 6 spaces after the point, the point (this is already 7), then there are 2 spaces to display before the point.

See this question , and this tutorial .

    
07.01.2017 / 04:02
2

This is just a way to format floating point output. According to with this tutorial , the number before the dot indicates the minimum number of numbers that will be printed before the dot (comma in some systems, but C follows the ISO standard, that is, it is a dot in the end). While the number after the comma indicates the precision of the number (decimal places or significant numbers in some cases).

#include <stdio.h>

main()
{
    printf("Float number: %3.2f\n", 3.14159);
}

//output: 3.14

According to with that other :

  

For example,% 10.3f will display the float using ten characters with   three digits after the decimal point. Notice that the ten characters   includes the decimal point, and a - sign if there is one. If the value   needs more space than the width specifies then the additional space is   used - width specifies the smallest space that will be used to display   the value.

Free Translation:

  

For example,% 10.3f will show the floating point with ten characters with   three digits after the decimal place. Note that the ten characters   include the three floating points and that a "-" will be shown if   there are any. If the value needs more space than the width   specifies then additional space is used - the width specifies the   Less space used to store value

Alternative:

The %g formats the number as many decimal places as you need (precision), giving the exponential format (scientific notation) preference to very small or very large numbers (1e-5 in place of .00001) and removing unnecessary zeros (176.12 instead of 176.1200). You can still specify the precision and the minimum width as %f .

    
07.01.2017 / 04:00