Table of Contents
1. Introduction
Rounding numbers to a specific number of decimal places is a common requirement in programming. In C++, this task becomes crucial when dealing with floating-point arithmetic, where precision and accuracy are key. This article will explore methods to round a floating-point number to 2 decimal places in C++. We will look at two distinct scenarios: Rounding the number, and displaying the rounded number.
Example:
Input: A floating-point number, say 5.82643
.
Expected Output: The number rounded to 2 decimal places, which would be 5.83
in this case.
Our Goal: To explore and compare different methods for rounding a decimal number to 2 decimal places in C++.
Please note that some methods might round down(floor) or round up(ceil) number as well.
2. Rounding a Number with 2 Decimal Places
Let’s see how to round a number to 2 decimals places in C++.
2.1 Using the round() Function
To round to 2 decimal places in C++, we can use round()
function. The round()
function returns the integer value that is nearest to the passed double, with halfway cases rounded away from zero.
We will multiply the given number by 100 and pass it to this function. The output of this will be divided by 100, and eventually, we will get the original number rounded up to 2 decimal places.
For example,
1 2 3 4 5 6 7 8 9 10 11 12 |
#include <iostream> #include <cmath> #include <iomanip> using namespace std; int main() { double a = 5.82643; float b = round(a * 100.0) / 100.0; cout << b; return 0; } |
Output:
1 2 3 |
5.83 |
2.2 Using the ceil() Function
The ceil()
function returns the smallest integer greater than the given integer. It will round up to the nearest integer. We can use this function to round to 2 decimal places in C++.
We will multiply the given number by 100 and pass it to this function. The output of this will be divided by 100, and eventually, we will get the original number rounded up to 2 decimal places.
For example,
1 2 3 4 5 6 7 8 9 10 11 12 |
#include <iostream> #include <cmath> #include <iomanip> using namespace std; int main() { double a = 5.82643; float b = ceil(a * 100.0) / 100.0; cout << b; return 0; } |
Output:
1 2 3 |
5.83 |
2.3 Using the floor() Function
The floor()
function returns the largest integer smaller than the given integer. It will round down to the nearest integer.
We will use this function as we did in the previous function. The original number will be rounded down to 2 decimal places.
See the code below.
1 2 3 4 5 6 7 8 9 10 11 12 |
#include <iostream> #include <cmath> #include <iomanip> using namespace std; int main() { double a = 5.82643; float b = floor(a * 100.0) / 100.0; cout << b; return 0; } |
Output:
1 2 3 |
5.82 |
2.4 Using the int Typecast
We can typecast variables to an integer value using the int
function. After typecasting, we can use the logic demonstrated in the previous two methods to carry out the rounding of numbers to 2 decimal places.
We will add 0.5
value to the converted value to round up the number.
For example,
1 2 3 4 5 6 7 8 9 10 11 12 13 |
#include <iostream> #include <cmath> #include <iomanip> using namespace std; int main() { double a = 5.82643; int c = (int)(a * 100 + .5); float b = c/100.0; cout << b; return 0; } |
Output:
1 2 3 |
5.83 |
2.5 Using sprintf and sscanf
Another approach is to use sprintf()
to convert the number into a string with 2 decimal places, then use sscanf()
to read it back into a double.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
#include <iostream> #include <cstdio> // Function using sprintf and sscanf to round to 2 decimal places double roundTo2DecimalPlaces(double number) { char buffer[20]; sprintf(buffer, "%.2f", number); sscanf(buffer, "%lf", &number); return number; } // Main program int main() { double number = 5.82643; // Rounding the number to 2 decimal places double roundedNumber = roundTo2DecimalPlaces(number); // Displaying the rounded number std::cout << "Original number: " << number << std::endl; std::cout << "Rounded to 2 decimal places: " << roundedNumber << std::endl; return 0; } |
1 2 3 4 |
Original number: 5.82643 Rounded to 2 decimal places: 5.83 |
sprintf()
formats the number as a string with 2 decimal places with format as%.2f
.sscanf*()
reads the string back into a double variable with format as%lf
.
This method is particularly useful in mixed C/C++ codebases or where traditional C-style string manipulation is preferred. It provides a workaround when standard C++ libraries are not available or suitable.
3. Display a Number with 2 Decimal Places
Let’s see how to display a number to 2 decimals places in C++.
3.1 Using the std::setprecision() Function with std:fixed
The combination of std::fixed and std::setprecision from the iomanip library is a straightforward way to display a number with 2 decimal places.
This method is useful for displaying output with the desired number of precision places. It specifies the precision in the standard stream. The number of digits is provided as an argument for this function.
To view numbers with 2 decimal places in C++, we provide 3 as the argument of this function.
For example,
1 2 3 4 5 6 7 8 9 10 |
#include <iostream> #include <iomanip> using namespace std; int main() { double a = 5.82643; std::cout << std::fixed << std::setprecision(2) << a << std::endl; return 0; } |
Output:
1 2 3 |
5.83 |
std::fixed
: Converts the floating-point number to fixed-point notation.std::setprecision(2)
: Sets the decimal precision to 2.
3.2 Using the printf() Function
We use the printf()
in C++ to display strings in the desired format. It is a part of the cstdio
header file. We use the %.2f
format specifier to display values rounded to 2 decimal places.
See the code below.
1 2 3 4 5 6 7 8 9 10 |
#include <iostream> #include <cstdio> using namespace std; int main() { double a = 5.82643; printf("%.2f",a); return 0; } |
Output:
1 2 3 |
5.83 |
3.3 Using the fprintf() Function
The fprintf()
function is similar to the printf()
function but can also write to any File stream object. To display rounded numbers, we specify the stdout
stream within this function.
For example,
1 2 3 4 5 6 7 8 9 10 |
#include <iostream> #include <cstdio> using namespace std; int main() { double a = 5.82643; fprintf(stdout, "%.2f",a); return 0; } |
Output:
1 2 3 |
5.83 |
4. Conclusion
In this article, we discussed several methods to round to 2 decimal places in C++. We can use the round()
,ceil()
and floor()
function from the cmath
header file. We can typecast decimal to integer and then convert it by performing some calculation. We can set the precision digits of the output stream with the setprecision()
function. We can also use the printf()
and fprintf()
function to round numbers to 2 decimal places by providing the %.2f
format specifier.