- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures

- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces

# C++ Program to Round a Number to n Decimal Places

Representing numbers in outputs is an interesting and also important task while we are writing programs in any language. For integer type (short, long, or medium) type data, it is easy to express the numbers as output. For floating point numbers (float or double type), sometimes we need to round them off to certain decimal places. For example, if we want to represent 52.24568 up to three decimal places, some pre-processing needs to be done. In this article, we shall cover a few techniques to represent a floating-point number up to a certain decimal placed by rounding it off.

Among different methods, using C like formatting strings, using precision parameters, and using the round() function present in the math library is important. Let us see them one by one. With proper syntaxes and code examples.

## Using Formatting String

In C language, we use the printf() function which denotes printing with formatting. To display some data using printf() functions, the formatting string needs to be specified beforehand. The same printf() function is also available in C++. To express numbers in certain decimal places, the formatting syntax will be like the below

### Syntax

Syntax for printf statements.

printf ( “%.<number of decimal place>f”, <floating point number> );

For example, if we want to display a floating point number variable NUM, up to 4 decimal places, the statement will be like −

printf ( “%.4f”, NUM );

### Example

#include <iostream> using namespace std; void solve( float number) { printf ( "%.3f", number ); } int main(){ cout << "Number 45.278586 up to 3 decimal places: "; solve( 45.278586 ); }

### Output

Number 45.278586 up to 3 decimal places: 45.279

In this example, we can see the given number is up to 6 decimal places. But we are displaying it up to 3 decimal places. And it is automatically converting to its nearest value when rounding off. However, this process has one drawback. We cannot dynamically change the decimal place value whenever we want. To overcome this issue we can follow another approach using C++ based setprecision() method.

## Using setprecision method

C++ has one special formatting function named setprecision() to set the precision value up to n decimal places. To use this method, we need to import the iomanip library. It also needs to specify we are using fixed decimal places. The syntax will be like the below −

### Syntax

Defining setprecision() method

include <iomanip> std::cout << std::fixed; std::cout << std::setprecision( <number of decimal places> ); std::cout << The_floating_point_number;

For example, if we want to display a floating point number variable NUM, up to 4 decimal places, the statement will be like −

include <iomanip> std::cout << std::fixed; std::cout << std::setprecision( 4 ); std::cout << NUM;

### Example

#include <iostream> #include <iomanip> using namespace std; void solve( float number, int place) { cout << fixed; cout << setprecision( place ); cout << number << endl; } int main(){ cout << "Number 45.278586 up to 3 decimal places: "; solve( 45.278586, 3); cout << "Number 45.278586 up to 4 decimal places: "; solve( 45.278586, 4); cout << "Number 45.278586 up to 5 decimal places: "; solve( 45.278586, 5); }

### Output

Number 45.278586 up to 3 decimal places: 45.279 Number 45.278586 up to 4 decimal places: 45.2786 Number 45.278586 up to 5 decimal places: 45.27859

This is an ideal way to represent numbers up to n decimal places. Sometimes for n = 0, we can use another method to round them off. This will convert the number into an integer. The approach is shown below −

## Using round() method

The ‘cmath’ library has one round() method to convert a number to its nearest integer. So this is converting the floating point number up to 0 decimal places. The syntax is like below.

### Syntax

Using round() method

include <cmath> float res = round ( <floating point number> );

For example, if we want to round a number 45.254 off to its nearest integer, the statement will be like the below.

include <cmath> float res = round ( 45.254 ); std::cout << res;

### Example

#include <iostream> #include <cmath> using namespace std; void solve( float number) { float res; res = round ( number ); cout << res << endl; } int main(){ cout << "Number 45.278586 to its nearest integer: "; solve( 45.278586 ); cout << "Number 89.7854 to its nearest integer: "; solve( 89.7854 ); cout << "Number -45.69 to its nearest integer: "; solve( -45.69 ); }

### Output

Number 45.278586 to its nearest integer: 45 Number 89.7854 to its nearest integer: 90 Number -45.69 to its nearest integer: -46

In this example, it is clear that to convert a floating point number to its nearest integer, the suitable and easy way is to use the round() function. This function takes the number as its parameter and returns the integer equivalent. In our example, there is a negative number - 45.69 here after rounding it off, it is turning it into -46 which is smaller than this. So round() method is not like floor() or ceil().

## Conclusion

Representing floating point numbers up to n decimal places has few approaches when we are writing code in C++. The very basic method is by using the printf() method with formatting string. However, for this method, the format string decimal places cannot be changed dynamically. To deal with that C++ iomanip library has the setprecision() method which takes the number of decimal places for which the floating point number is being rounded off. Sometimes we need to round a floating point number to its nearest integer (0 decimal places) In such a scenario we can use the round() method present inside the cmath library in C++.

- Related Articles
- Java Program to Round a Number to n Decimal Places
- Swift Program to Round a Number to n Decimal Places
- Kotlin Program to Round a Number to n Decimal Places
- How to round a number to n decimal places in Java
- I want to round a number to 2 decimal places in SAPUI5. Could anyone help?
- C++ Program to compute division upto n decimal places
- Trying to round a calculation to two decimal places in a new column with MySQL?
- Python - Round number of places after the decimal for column values in a Pandas DataFrame
- Java program to round a number
- How to round correlation values in the correlation matrix to zero decimal places in R?
- How to format number to 2 decimal places in MySQL?
- How can I round a number to 1 decimal place in JavaScript?
- The decimal expansion of the rational number will $\frac{14587}{1250}$ terminate after:(A) one decimal place(B) two decimal places(C) three decimal places(D) four decimal places
- Java Program to format to 2 decimal places in a 10-character field
- How to round the decimal number to the nearest tenth in JavaScript?