C++

Round the Floating-Point Number to Two Decimal Places in C++

In C++, a floating-point number is a numeric data type that is used to represent the real numbers with a fractional part. The floating-point numbers are typically used to store and perform computations on values that require a higher level of precision than the integer types can provide.

C++ provides two floating-point data types: float and double. The floating-point numbers can be expressed in various forms including decimal notation and scientific notation.

Rounding the Floating Point Numbers

Rounding the floating-point numbers in C++ is often necessary to manage the precision and control the number of decimal places in the output. Rounding allows you to present the numerical values in a more readable or standardized format. Because the floating-point numbers are represented in binary form, they have limited precision.

Rounding can help manage the number of significant digits or decimal places, ensuring that the output does not include excessive precision or insignificant digits.

It’s important to note that rounding introduces a level of approximation, and a precision loss may occur. The best rounding approach is determined by the unique application and the required level of accuracy. Different rounding methods such as rounding to the nearest integer, rounding up, rounding down, or rounding to a specific decimal place can be employed based on the specific requirements and constraints of the problem at hand.

Using the rounding techniques, you can manage the representation and presentation of the floating-point numbers in a way that aligns with the desired precision, readability, and compliance with specifications.

In this article, we will go through some of the rounding methods to round a floating-point number to two decimal places.

Method 1: Using the Std::Round

The first method that we utilize here is the “std::round” to round a floating-point number. The step-by-step guide to the program is provided in the following:

#include <iostream>

#include <cmath>

#include <iomanip>

int main()

{

  double numb = 5.12345;

  double roundedN = std::round(numb * 100) / 100;

  std::cout << "Original number is: " << numb << std::endl;

  std::cout << "Rounded number is: " << roundedN << std::endl;

  return 0;

}

The initial lines of the code include the necessary header files like <iostream, <cmath>, and <iomanip>.

The <iostream> header provides the functionality for input/output operations such as reading from the standard input (std::cin) and writing to the standard output (std::cout).

The C++ <cmath> header file is a component of the C++ Standard Library and contains a set of mathematical operations and constants. It enables you to conduct the different computations such as fundamental mathematics, trigonometry, the use of exponent logarithms, rounding, etc.

The <iomanip> header file in C++ provides facilities to manipulate the format of the input and output streams. It allows you to control the various aspects of the output such as setting the precision of floating-point numbers, controlling the field width, aligning the text, and more.

By including these header files, you gain an access to the functionalities that they provide which allows you to perform the input/output operations, utilize the mathematical functions, and manipulate the output format in your C++ program.

In this code snippet, a double variable named number is initialized with the value of 5.12345. The numb * 100 multiplication shifts the decimal point two positions to the right, thereby multiplying the integer by 100. The result is then passed to the “std::round” function which rounds the value to the nearest whole number. In the end, dividing the rounded value by 100 is done to move the decimal point back to its original position, effectively achieving the desired rounding to two decimal places.

These lines use the “std::cout” to print the original number and the rounded number to the console. The << operator is used to concatenate the output and “std::endl” is used to insert a newline character.

The generated output is shown in the following:

Method 2: Formatting Using Std::Fixed and Std::Setprecision

For the second illustration, let’s work with the “std::fixed” and “std::setprecision” methods to round a floating point number.

#include <iostream>

#include <iomanip>

int main() {

  double digit = 13.81728;

  std::cout << "Original number is: " << digit << std::endl;

  std::cout << std::fixed << std::setprecision(2);

  std::cout << "Rounded number is: " << digit << std::endl;

  return 0;

}

We include the necessary header files. The <iostream> is included for input/output operations, and <iomanip> is included to manipulate the output format.

In the code, a double variable named “number” is initialized with the value of 13.81728. The line std::cout << “Original number: ” << digit << std::endl; uses “std::cout” to print the “Original number:” string followed by the value of the number to the console. The << operator is used to concatenate the output.

Then, in the next lines, the program modifies the output format for the subsequent output. The usage of the “std::fixed” manipulator guarantees that the floating-point number is presented in a fixed-point notation. The std::setprecision(2) manipulator sets the precision to two decimal places. This means that the subsequent floating-point numbers that are printed on the console have two decimal places.

The line std::cout << “Rounded number: ” << digit << std::endl; uses “std::cout” to print the “Rounded number:” string followed by the value of the number to the console. Since the precision is set to two decimal places, the value of the number is displayed with two decimal places.

When we run this code, the output displays the original number and the rounded number with two decimal places.

Please note that the “std::fixed” and “std::setprecision” manipulators affect the formatting of subsequent floating-point numbers until they are changed again.

Method 3: Using a Helper Function for Rounding

In C++, there are several helper functions available in different libraries to round a floating-point number. One of the commonly used helper functions is std::round() which we will use in this example.

#include <iostream>

#include <cmath>

#include <iomanip>

  double roundToTwoDecimalPlaces(double figure) {

  return std::round(figure * 100) / 100;

 }

int main() {

  double figure = 100.213576689;

  double roundedfigure = roundToTwoDecimalPlaces(figure);

  std::cout << "Original number is: " << figure << std::endl;

  std::cout << "Rounded number is: " << roundedfigure << std::endl;

  return 0;

}

This code defines a separate function called “roundToTwoDecimalPlaces” that takes a double figure as an argument. Inside the function, the decimal point is moved two places to the right by multiplying the given number by 100. The “std::round” function is then used to round the multiplied value to the nearest whole number.

Finally, the rounded value is divided by 100 to shift the decimal point back to its original position which results in the rounded figure with two decimal places. The rounded figure is returned from the function.

In the main() function, a double variable named “figure” is initialized with the value of 100.213576689. The figure is then sent as an input to the “roundToTwoDecimalPlaces” function, and the function’s result value is saved in the “roundedfigure” variable. Finally, the original number and the rounded number are printed to the console using “std::cout”. The << operator is used to concatenate the output and “std::endl” is used to insert a newline character.

Here is the output:

Conclusion

Rounding a floating point number to two decimal places can be done using various techniques in C++. We explained the three strategies in this article. The first approach rounds a floating point value using “std::round”. The second method involves the utilization of the “std::fixed” and “std::setprecision” methods. Whereas the last method uses a helper function to round a floating point number to two decimal places.

About the author

Omar Farooq

Hello Readers, I am Omar and I have been writing technical articles from last decade. You can check out my writing pieces.