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++.

Updated on: 17-Oct-2022

16K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements