Home > System Tutorial > LINUX > body text

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

WBOY
Release: 2024-04-15 11:25:20
forward
788 people have browsed it

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>
Copy after login

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!

source:linuxprobe.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!