Doubt with float

Asked

Viewed 481 times

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 would like to know what serves an entire number before the point. For example, %7.2f or %3.2f. If the number that comes after the point is used to limit the number of decimal places, then what is the number that comes before the point?

3 answers

4


The number that comes after the % and before the . is the number of characters minimum that the number goes into 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 way out:

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

Note that in test 2, enter the first ' And the second, there are eight characters. Obviously, if the resulting string were to exceed this, it would have as many characters as needed (such as in test 1).

Putting a zero right after the %, instead of filling in with spaces, filling occurs with zeros, such as in test 3.

See here working on ideone.

Relevant reference: http://www.cplusplus.com/reference/cstdio/printf/

2

It is only a way to format the output of floating points. According with this tutorial, the number before the point indicates the minimum quantity of numbers that will be printed before the dot (comma on some systems, but C follows the ISO standard, that is, it is a point at the end of the accounts). While the number after the comma indicates the accuracy 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

Accordingly 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 not there is any one. If the value needs more space than the width then specifies additional space is used - the width specifies the smaller space used to store the value

Alternative:

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

2

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

For example, the instruction: printf("%9.6f", myFloat) specifies that the width of the display digits will be 9 spaces. This means that they will have 6 spaces after the point, the point (this already gives 7) finally, 2 spaces to display before the point.

Behold that question, and this tutorial.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.