How to use setprecision in C++? Last Updated : 17 May, 2024 Comments Improve Suggest changes Like Article Like Report In C++, we have a std::setprecision function which is a part of the <iomanip> library. It is used to set the number of decimal places to be displayed for a floating-point number. In this article, we will learn how to use the setprecision function in C++. Example: Input: double num = 3.142857142857; Output: 3.14 // precision set to 2 decimal placesUsing setprecision() Function in C++In C++, the default precision of floating-point numbers can be changed by specifying the number of decimal places to be output inside the setprecision() method. It is applied to the whole number, including the integer and fractional parts, and uses scientific notation when the number is large and exceeds the specified precision. Syntax of setprecision in C++cout << setprecision(int n) << num;Here, n is the number of decimal places we want to output. C++ Program to Show the Use of setprecision()The below example demonstrates how we can use the setprecision() method to set the precision of floating-point numbers in C++. C++ // C++ Program to demonstrate the use of setprecision #include <iomanip> #include <iostream> using namespace std; int main() { // Initializing a decimal number double num = 3.142857142857; // Displaying the number before setting precision cout << "Before setting the precision: " << num << endl; // Setting the precision to 3 and displaying the number cout << "After setting the precision to " "3: " << setprecision(3); cout << num << endl; // Setting the precision to 5 and displaying the number cout << "After setting the precision to " "5: " << setprecision(5); cout << num << endl; return 0; } OutputBefore setting the precision: 3.14286 After setting the precision to 3: 3.14 After setting the precision to 5: 3.1429Time Complexity: O(1)Auxiliary Space: O(1) Note: The setprecision() function will only alter the precision of the output and not the actual value of the floating-point number, default value is fixed and is an inherent part of the datatype. Comment More infoAdvertise with us Next Article How to use setprecision in C++? R rahulkatix28 Follow Improve Article Tags : C++ Programs C++ cpp-input-output cpp-manipulators CPP Examples +1 More Practice Tags : CPP Similar Reads How to use the setw Manipulator in C++? In C++, the std::setw manipulator, which stands for âset widthâ, is a function provided by the <iomanip> library. The width of the field in which the output of the subsequent value will be shown can be adjusted using the setw() manipulator. In this article, we will learn how we can use the set 2 min read Measure execution time with high precision in C/C++ Execution time : The execution time or CPU time of a given task is defined as the time spent by the system executing that task in other way you can say the time during which a program is running. There are multiple way to measure execution time of a program, in this article i will discuss 5 differen 8 min read Convert Float to String In C++ In this article, we learn how we can convert float to string in C++ using different methods: Using the to_string()Using stringstreamUsing MacrosUsing lexical_cast from the boost library1. Using to_string() The to_string() method takes a single integer variable or other data type and converts it into 3 min read How to Round a Double to an int in C++? In C++, the double data type is used to store floating-point numbers with double precision. In this article, we will learn how to round a double value to an int in C++. Example: Input: double val= 2.6 Output: Integer for double value 2.5 is: 3 Rounding a Double to an Int in C++To round a double valu 2 min read How Do I Print a Double Value with Full Precision Using cout? The double value in C++ has a precision of up to 15 digits but while printing it using cout, it only prints six significant digits. In this article, we will learn how to print the double value with full precision. For Example, Input: double var = 12.3456789101112 Output: var = 12.3456789101112Print 2 min read How to Convert Float to int in C++? In C++, the float is a short term for floating point value that is used to define numeric values with decimal points. The floating point numbers can be converted to integers using the process called type casting. In this article, we will learn how to convert a float value to an int in C++. Example: 2 min read How to Convert wstring to double in C++ In C++, std::wstring is a type of string where each character is of a wide character type. Converting wstring to a double in C++ can be a common requirement especially when dealing with the inputs that are stored as unicode characters. In this article, we will learn how to convert a wstring to a dou 2 min read How to Assign Negative Infinity in C++? In C++, the infinity is written as inf. We get infinity as a result when a positive number is divided by a null value or when a value is much greater and cannot be stored in 64-bit. In C++, positive infinity is defined in many libraries but the negative infinity is not defined. In this article, we w 2 min read How to Catch Floating Point Errors in C++? In C++, a part of the code that may throw exceptions is enclosed in try-and-catch blocks to handle them when they arise. We can also use try...catch to catch floating point errors but it involves a bit different approach in comparison to catching standard exceptions. In this article, we will look at 2 min read ecvt() in C/C++ with Examples This function ecvt() converts the double value to string value and returns a pointer to it. The library function defined in stdlib.h header file. Syntax: char * ecvt (double value, int num, int * dec, int * sign); Parameters: double value: It is the double value which will convert into string.int nu 2 min read Like