How to Round a Number to 2 decimal places in C++

Round to 2 decimal places in C++

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,

Output:

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,

Output:

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.

Output:

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,

Output:

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.

  • 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,

Output:

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

Output:

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,

Output:

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.

Was this post helpful?

Leave a Reply

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