SetPrecision in C++ With Examples

SetPrecision in C++ With Examples

3 mins read883 Views Comment
Updated on Feb 6, 2023 19:07 IST

SetPrecision is an in-built C++ tool to manipulate floating-point values. Learn about how this function works with two easy examples.

2023_02_SetPrecision-Function-in-C-plus-plus.jpg

SetPrecision in C++ is a built-in tool to modify floating-point numbers. It controls the number of decimal places displayed for floating-point values. Whether you’re working with mathematical calculations, scientific simulations, or financial models, setprecision() is a valuable tool for ensuring that your output is accurate and informative.

This tutorial will discuss the setprecision() function in C++ and explore how it works through code examples. We will be covering the following sections:

  1. SetPrecision in C++
  2. How SetPrecision in C++ Works
  3. Examples of Using SetPrecision() in C++

SetPrecision in C++

In C++, the setprecision function, given by setprecision(), is part of the <iomanip> library and is used to set the precision (number of digits after the decimal point) for output operations performed by the cout object.

The setprecision() function takes an integer argument. It specifies the number of digits to display after the decimal point.

Syntax

The syntax to declare setprecision() in C++ is below.

 
setprecision(int n)
Copy code

As you can see, the setprecision() function requires only one argument, represented by n. This argument specifies the number of significant digits displayed for a floating-point number. The argument must be an integer and is necessary for the function to work.

Return Value

The return value of the setprecision() function in C++ is not specified. The purpose of this function is to act as a stream manipulator, modifying the output stream and not returning a value.

Exceptions

The setprecision() function does not typically generate exceptions. If the function throws an exception, the output stream remains in a valid state.

How SetPrecision in C++ Works

The setprecision() function in C++ controls the number of significant digits displayed after the decimal point for floating-point numbers in the output stream. You can prevent information loss by setting a precise value with n significant digits.

For instance, when storing the value of pi with an infinite number of digits, some digits after the decimal may automatically reduce in a 4-bit or 8-bit memory architecture.

Using the setprecision() function with an argument of n, the value of pi should appear with n significant digits without information loss.

For example, setprecision(3) would display the value of pi as 3.14. To ensure that the correct number of decimal places show in the output, use the fixed keyword should before the setprecision() function. This will display the required number of decimal places in the output.

Examples of Using SetPrecision() in C++

Here are two examples.

Example 1

 
#include <iostream>
#include <iomanip>
int main() {
double value = 123.456789;
std::cout << "Default precision: " << value << std::endl;
std::cout << std::setprecision(3) << "Precision set to 3: " << value << std::endl;
std::cout << std::setprecision(5) << "Precision set to 5: " << value << std::endl;
return 0;
}
Copy code

In this example, value is a floating-point number with a value of 123.456789. The cout stream displays the value of the variable value. The default precision is set to 3 and 5 using the setprecision() function. The output of this program will be:

Default precision: 123.457
Precision set to 3: 123
Precision set to 5: 123.46

The setprecision() function sets the number of significant digits after the decimal point for the output of value. The function returns an object of type ios_base&, which is used to set the precision for subsequent output operations performed by the cout object.

Example 2

 
#include <iostream>
#include <iomanip>
#include <cmath>
int main() {
double pi = 3.14159265358979;
std::cout << std::fixed << std::setprecision(2);
std::cout << "Pi with 2 decimal places: " << pi << std::endl;
std::cout << "Sine of Pi/2 with 4 decimal places: " << std::sin(pi / 2) << std::endl;
return 0;
}
Copy code

In this example, pi is a variable that stores the value of pi with a high degree of precision. The fixed manipulator displays the value with a fixed number of decimal places, and setprecision() is used to set the number of decimal places to 2. The sin function from the <cmath> library is used to calculate the sine of pi / 2 with a precision of 4 decimal places.

The output of this program will be:

Pi with 2 decimal places: 3.14
Sine of Pi/2 with 4 decimal places: 1.00

As you can see, the setprecision() function controls the number of decimal places displayed for both pi and the result of the sin function. The fixed manipulator is used to ensure that the decimal point does not change position for the output of pi and the sin function.

Endnotes

In conclusion, the setprecision() function in C++ is a powerful tool for controlling the number of decimal places displayed in the output of floating-point numbers. The function is easy to use and can help prevent information loss by allowing you to set a specific number of significant digits for floating-point numbers.

Hope this article helped you grasp the concept of setprecision() in C++. Explore our C++ articles to learn more about the language and consolidate your knowledge of the fundamentals.  

Contributed by – Prerna Singh

Read More

What are Getline Function in C++?
What are Getline Function in C++?
In C++, getline() is a standard library function in C++ that is used to read a line of text from an input stream and store it as a string. Getline...read more
Working with Pairs in C++
Working with Pairs in C++
In this article, we will learn what are pairs in C++, and how to initialize pairs in C++. Some common C++pairs STL functions. Later in the article, we will also...read more
Array of Strings in C++
Array of Strings in C++
A String Array in C++ is a collection of strings, stored in an array data structure.
About the Author

This is a collection of insightful articles from domain experts in the fields of Cloud Computing, DevOps, AWS, Data Science, Machine Learning, AI, and Natural Language Processing. The range of topics caters to upski... Read Full Bio