According to the explanation on the C + + official website, this function has no specified return type, and it is used to set the exact decimal place on the output operation.
This function is only used to modify the data stream if the return value is not specified.
Having said so much, what I want to express is that when we need to save the exact decimal (assign the value of a to b), we can’t use the setprecision function.
Don’t worry. I have a solution
1. Use the stringstream class
Here we use the first method. We can see that the first explanation is to set the format flag of the stream to the form of the parameter fmtfl
But what is the parameter fmtflags fmtfl? Keep following in. You can see
You can see here that fixed means to set the decimal to a fixed-point representation
Then SS. Precision (3) is to keep 3 bits
Put together means to keep 3 significant digits after the decimal point.
As for SS. Str (), you can see the following explanation on the official website to get the content saved in the string stream. Note that the return bit STD:: string type is:
2. Manual calculation, end floor function
The idea of this algorithm is to multiply the number by 1000 to get three decimal places, and then what does + 0.5 mean?
If you think about it, a * 1000 = 1123.456789, right? Generally, we need to round the significant bits,
After a * 1000, + 0.5 is automatically rounded? And then in the use of the floor function rounding, and then divided by 1000 is not perfect to achieve it?
Summary: the first method is suitable for processing a large amount of data at one time. For example, if you want to write a lot of data into TXT at one time.
The second method is more suitable for immediate calculation and use, fast and simple.