Do a problem to encounter the title that retains two decimal places, what write on the textbook is much and miscellaneous, what check on the net comes also is a pile of content needs screening, 눈_눈 or oneself sum up.
Let’s start with C++ code
#include <iomanip> // Don't forget the header file
Copy the code
// The first way
cout<<setiosflags(ios::fixed)<<setprecision(2);
Copy the code
// The second way
cout.setf(ios::fixed);
cout<<setprecision(2);
Copy the code
// The third way
cout<<fixed<<setprecision(2);
Copy the code
- I want to keep a number of decimal places in the parentheses for setprecision(n).
- The first two are the same, and the third is a simplification.
- Write the above statement once and it will be valid for all subsequent numbers.
And then the C code
Personally, THE output of C is relatively simple and crude.
- %f formats output floating-point data by adding “. N “after %. Such as:
#include <stdio.h>
int main(a)
{
float PI=3.1415926;
float R=5.3;
printf("Area = %.2f\n", PI * R * R); // Output: area = 88.25
printf("Area = %f\n", PI * R * R); // Output: area = 88.247337
printf("Area = %.8f\n", PI * R * R); // Output: area = 88.24733734
return 0;
}
Copy the code
- The “.2 “in %.2f\n is reserved for two decimal places
- // If you do not set the number of decimal places to be reserved, the default is six
If you know how to use it, you can see it here!
For a deeper understanding, go to 눈_눈
1. First explain “statement write once on the line, after the number is valid”. After setting to keep two decimal places after s, redeclare another number, and the output still shows two decimal places. So setting the precision statement only needs to be written once.
#include <iostream>
#include <iomanip> // Set the required header file
using namespace std;
int main(a)
{
double s=12.345;
cout<<setiosflags(ios::fixed)<<setprecision(2);
cout<<s<<endl;/ / output 12.35
float pi=3.14159;
cout<<pi<<endl;/ / output 3.14
return 0;
}
Copy the code
2. Setprecision (n) function: Controls the number of valid digits displayed in floating point numbers.
As you can see, only setPrecision (n) controls the retention of several significant digits.
- As can be seen from the 8-9 lines of code, also only once can be written.
- As you can see from lines 8 to 10, it is only the rounding that changes the display method of the number, not the original number. Common sense tells us that if the number 12.345 itself changes, that is, if the two significant digits change to 12, then changing from a two significant digit to a four significant digit will change to 12.00, not 12.34.
- Lines 11-12 show that if you want to keep too many, you won’t be able to fill in the zeros (see below for ways to fill in the zeros).
- As you can see in line 13, if you have more digits in front of the decimal point than you want to keep, you use scientific notation.
2.5 Add a littleshowpoint This is a boring operation found in the process of trying, if12.34Keep two significant digits and the display will be displayed12But you can make it display by doing this12.Hahahahahahahaha notice the “. Years (•̀ω•́)ゝ!
double s=12.345;
cout<<s<<endl;// Output the original number 12.345
cout<<setprecision(2);
cout<<s<<endl;// The output retains the two significant digits 12
cout.setf(ios::showpoint);//或者写cout<<setiosflags(ios::showpoint);
cout<<s<<endl;// The output retains two significant digits plus a decimal point 12.
Copy the code
3. Fixed setprecision(n) and fixed can control the number of places behind the decimal point. Just add any of the following statements.
cout<<setiosflags(ios::fixed);
cout.setf(ios::fixed);
cout<<fixed;
Copy the code
And then you see, if you want to keep more digits than the original decimal number, you add zeros.
I am a girl Ann, and my dream is to be a program girl…
Have CSDN account to help me point a like or comment on it, do not want to also do not force. You point praise comment I just can get integral, just can rise grade, grade high ability change good-looking skin to blog 🥰🥰