Show two digits after decimal point in C++

In this tutorial, We are going to learn about how to show floating numbers up to two digits decimal places using C++.  To have a better understanding of the problem we must consider some examples.

For Example:
Let us take some floating numbers
a = 12.34567 
b= 100.1111910

So now the output will become 
a = 12.37 
b= 100.11

Now to solve this problem we are going to use a simple function named setprecision () which requires <iomanip> header file.

First of all, let us understand that about the <iomanip > header file and what it does?

The header file <iomanip>  stands for the input/output library of the C++ Standard Library. It is a library that helps us in the manipulation of the output of any C++ program.

Syntax for the setprecision() function

setprecision(int n)
n is the only parameter that is passed through this function. It is the integer argument for the floating-point precision that needs to be set.

This method returns nothing it's just a manipulator stream.

Implementation for showing two digits after the decimal point in C++

#include <iomanip>
#include <iostream>

using namespace std;

int main()

// Let us consider the value of pi 
float a = 3.142857142857;
// take another number
float b=  3.1211112311;

// Using setprecision()
cout << setprecision(3);

cout <<a <<" "<< b<< endl;

return 0;


3.14 3.12

Leave a Reply

Your email address will not be published. Required fields are marked *