Home  >  Article  >  System Tutorial  >  Detailed explanation of the double type in C++ and how to retain three decimal points

Detailed explanation of the double type in C++ and how to retain three decimal points

WBOY
WBOYforward
2024-04-15 11:25:20796browse

Detailed explanation of the double type in C++ and how to retain three decimal points

We know that in C language, if the output result is required to retain three decimal places, we can use the pritf() function to easily solve the problem. But C's output operator

I will use an example as an explanation:

It is known that the coordinates of the two endpoints of the line segment are A (xa, ya), B (xb, yb). Find the length of the line segment AB and keep it to 3 decimal places.

Sample input: 1 1

twenty two

Sample output: 1.414

code show as below:
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) );
    cout
<p>Many people will not understand the following code, </p>
<pre class="brush:php;toolbar:false">cout
<p> Let’s focus on the following: </p>
<div style="margin-top: 2em; margin-bottom: 1em;"><span style="color: #1e1e1e; letter-spacing: 2px; border-left: #FF3030 3px solid; border-right: #FF3030 3px solid; padding-left: 8px; padding-right: 8px; font-size: 12pt;"><strong>Set decimal point precision</strong></span></div>
<p> Set the decimal precision of floating point values ​​to be used for formatted output operations. </p>
<pre class="brush:php;toolbar:false">int main(int argc, char** argv) {
    double a=3.1415926;
    cout 
<p>If you have any questions, please leave a message. If you need to reprint, please indicate the source: http://www.cnblogs.com/wongyi/p/8204210.html</p>

The above is the detailed content of Detailed explanation of the double type in C++ and how to retain three decimal points. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:linuxprobe.com. If there is any infringement, please contact admin@php.cn delete