How to keep three decimal places of double type in C++
This article mainly introduces the "C++ double type how to retain three decimal places" related knowledge, editor through an actual case to show you the process of operation, the method of operation is simple and fast, practical, I hope this "C++ double type how to retain three decimal places" article can help you solve the problem.
Let me take an example as an example to explain:
Given that the two endpoint coordinates of the line segment are A (xa,ya) and B (xb,yb), the length of the line segment AB is calculated and reserved to 3 places after the decimal point.
Sample input: 1 1
2 2
Sample output: 1.414
The code is as follows: int main (int argc, char** argv) {double xa,ya,xb,yb,result; cin > > xa > > ya > > xb > > yb; result=sqrt ((xb-xa) * (xb-xa) + (yb-ya) * (yb-ya)); coutreturn 0;}
Many people will not understand the following code
Cout
Let's focus on the following:
Set the precision of decimal point
Sets the decimal precision of the floating-point value to be used to format the output operation.
Int main (int argc, char** argv) {double axiom 3.1415926; cout return 0;} that's all for the introduction of "how to keep three decimal places for double types in C++". Thank you for reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.