Get the App
SLTechnology News&Howtos  ›  Development  › 

How to keep three decimal places of double type in C++

Shulou Source: shulou.com Published: 2022-06-01 17:28:58 09月14日 Update

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.

Tags: Decimal decimal type Cumulative knowledge code precision line segment industry output different practical two examples content coordinates practicality practice article method Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Huawei MariaDB Linux Microsoft Docker